You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

115 lines
4.5 KiB

9 years ago
  1. #!/bin/bash
  2. # Copyright 2014 The Kubernetes Authors All rights reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. set -o errexit
  16. set -o nounset
  17. set -o pipefail
  18. # Caller should set in the ev:
  19. # MASTER_IP - this may be an ip or things like "_use_gce_external_ip_"
  20. # DNS_DOMAIN - which will be passed to minions in --cluster_domain
  21. # SERVICE_CLUSTER_IP_RANGE - where all service IPs are allocated
  22. # MASTER_NAME - I'm not sure what it is...
  23. # Also the following will be respected
  24. # CERT_DIR - where to place the finished certs
  25. # CERT_GROUP - who the group owner of the cert files should be
  26. cert_ip="${MASTER_IP:="${1}"}"
  27. master_name="${MASTER_NAME:="kubernetes"}"
  28. service_range="${SERVICE_CLUSTER_IP_RANGE:="10.0.0.0/16"}"
  29. dns_domain="${DNS_DOMAIN:="cluster.local"}"
  30. cert_dir="${CERT_DIR:-"/srv/kubernetes"}"
  31. cert_group="${CERT_GROUP:="kube-cert"}"
  32. # The following certificate pairs are created:
  33. #
  34. # - ca (the cluster's certificate authority)
  35. # - server
  36. # - kubelet
  37. # - kubecfg (for kubectl)
  38. #
  39. # TODO(roberthbailey): Replace easyrsa with a simple Go program to generate
  40. # the certs that we need.
  41. # TODO: Add support for discovery on other providers?
  42. if [ "$cert_ip" == "_use_gce_external_ip_" ]; then
  43. cert_ip=$(curl -s -H Metadata-Flavor:Google http://metadata.google.internal./computeMetadata/v1/instance/network-interfaces/0/access-configs/0/external-ip)
  44. fi
  45. if [ "$cert_ip" == "_use_aws_external_ip_" ]; then
  46. cert_ip=$(curl -s http://169.254.169.254/latest/meta-data/public-ipv4)
  47. fi
  48. if [ "$cert_ip" == "_use_azure_dns_name_" ]; then
  49. cert_ip=$(uname -n | awk -F. '{ print $2 }').cloudapp.net
  50. fi
  51. tmpdir=$(mktemp -d --tmpdir kubernetes_cacert.XXXXXX)
  52. trap 'rm -rf "${tmpdir}"' EXIT
  53. cd "${tmpdir}"
  54. # TODO: For now, this is a patched tool that makes subject-alt-name work, when
  55. # the fix is upstream move back to the upstream easyrsa. This is cached in GCS
  56. # but is originally taken from:
  57. # https://github.com/brendandburns/easy-rsa/archive/master.tar.gz
  58. #
  59. # To update, do the following:
  60. # curl -o easy-rsa.tar.gz https://github.com/brendandburns/easy-rsa/archive/master.tar.gz
  61. # gsutil cp easy-rsa.tar.gz gs://kubernetes-release/easy-rsa/easy-rsa.tar.gz
  62. # gsutil acl ch -R -g all:R gs://kubernetes-release/easy-rsa/easy-rsa.tar.gz
  63. #
  64. # Due to GCS caching of public objects, it may take time for this to be widely
  65. # distributed.
  66. # Calculate the first ip address in the service range
  67. octects=($(echo "${service_range}" | sed -e 's|/.*||' -e 's/\./ /g'))
  68. ((octects[3]+=1))
  69. service_ip=$(echo "${octects[*]}" | sed 's/ /./g')
  70. # Determine appropriete subject alt names
  71. sans="IP:${cert_ip},IP:${service_ip},DNS:kubernetes,DNS:kubernetes.default,DNS:kubernetes.default.svc,DNS:kubernetes.default.svc.${dns_domain},DNS:${master_name}"
  72. curl -L -O https://storage.googleapis.com/kubernetes-release/easy-rsa/easy-rsa.tar.gz > /dev/null 2>&1
  73. tar xzf easy-rsa.tar.gz > /dev/null
  74. cd easy-rsa-master/easyrsa3
  75. (./easyrsa init-pki > /dev/null 2>&1
  76. ./easyrsa --batch "--req-cn=${cert_ip}@$(date +%s)" build-ca nopass > /dev/null 2>&1
  77. ./easyrsa --subject-alt-name="${sans}" build-server-full "${master_name}" nopass > /dev/null 2>&1
  78. ./easyrsa build-client-full kubelet nopass > /dev/null 2>&1
  79. ./easyrsa build-client-full kubecfg nopass > /dev/null 2>&1) || {
  80. # If there was an error in the subshell, just die.
  81. # TODO(roberthbailey): add better error handling here
  82. echo "=== Failed to generate certificates: Aborting ==="
  83. exit 2
  84. }
  85. mkdir -p "$cert_dir"
  86. cp -p pki/ca.crt "${cert_dir}/ca.crt"
  87. cp -p "pki/issued/${master_name}.crt" "${cert_dir}/server.crt" > /dev/null 2>&1
  88. cp -p "pki/private/${master_name}.key" "${cert_dir}/server.key" > /dev/null 2>&1
  89. cp -p pki/issued/kubecfg.crt "${cert_dir}/kubecfg.crt"
  90. cp -p pki/private/kubecfg.key "${cert_dir}/kubecfg.key"
  91. cp -p pki/issued/kubelet.crt "${cert_dir}/kubelet.crt"
  92. cp -p pki/private/kubelet.key "${cert_dir}/kubelet.key"
  93. CERTS=("ca.crt" "server.key" "server.crt" "kubelet.key" "kubelet.crt" "kubecfg.key" "kubecfg.crt")
  94. for cert in "${CERTS[@]}"; do
  95. chgrp "${cert_group}" "${cert_dir}/${cert}"
  96. chmod 660 "${cert_dir}/${cert}"
  97. done