Skip to content
This repository has been archived by the owner on Jun 23, 2022. It is now read-only.

Commit

Permalink
cmd/kube-client-agent: add new option csrName to specify the csr obje…
Browse files Browse the repository at this point in the history
…ct's name

This change should not change any existing behaviour. If the field '--csrName' is
not specified then the commonname will be used for the object's name (what happens today).
When specified, the objects name will be taken accordingly. This is useful
when multiple CSR objects need to be created for the same 'commonname'.
  • Loading branch information
Rajat Chopra committed Jan 25, 2019
1 parent 52278c2 commit d9ad2cb
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
7 changes: 7 additions & 0 deletions cmd/kube-client-agent/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ var (
ipAddresses string
assetsDir string
kubeconfig string
csrName string
}
)

Expand All @@ -35,6 +36,7 @@ func init() {
requestCmd.PersistentFlags().StringVar(&requestOpts.orgName, "orgname", "", "CA private key file for signer")
requestCmd.PersistentFlags().StringVar(&requestOpts.dnsNames, "dnsnames", "", "Comma separated DNS names of the node to be provided for the X509 certificate")
requestCmd.PersistentFlags().StringVar(&requestOpts.ipAddresses, "ipaddrs", "", "Comma separated IP addresses of the node to be provided for the X509 certificate")
requestCmd.PersistentFlags().StringVar(&requestOpts.csrName, "name", "", "Name for the CertificateSigningRequest object, defaults to value provided by 'commonname' arg")
requestCmd.PersistentFlags().StringVar(&requestOpts.assetsDir, "assetsdir", "", "Directory location for the agent where it stores signed certs")
requestCmd.PersistentFlags().StringVar(&requestOpts.kubeconfig, "kubeconfig", "", "Path to the kubeconfig file to connect to apiserver. If \"\", InClusterConfig is used which uses the service account kubernetes gives to pods.")
}
Expand Down Expand Up @@ -75,12 +77,17 @@ func runCmdRequest(cmd *cobra.Command, args []string) error {
}
}

csrName := requestOpts.csrName
if csrName == "" {
csrName = requestOpts.commonName
}
config := agent.CSRConfig{
CommonName: requestOpts.commonName,
OrgName: requestOpts.orgName,
DNSNames: strings.Split(requestOpts.dnsNames, ","),
IPAddresses: ips,
AssetsDir: requestOpts.assetsDir,
CSRName: csrName,
}
a, err := agent.NewAgent(config, requestOpts.kubeconfig)
if err != nil {
Expand Down
8 changes: 6 additions & 2 deletions pkg/certagent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ type CSRConfig struct {
// AssetsDir is the directory location where certificates and
// private keys will be saved
AssetsDir string `json:"assetsDir"`

// CSRName is the name of the CertificateSigningRequest object
// that will be created
CSRName string `json:"csrName"`
}

// CertAgent is the top level object that represents a certificate agent.
Expand Down Expand Up @@ -97,7 +101,7 @@ func GenerateCSRObject(config CSRConfig) (*capi.CertificateSigningRequest, error

csr := &capi.CertificateSigningRequest{
TypeMeta: metav1.TypeMeta{Kind: "CertificateSigningRequest"},
ObjectMeta: metav1.ObjectMeta{Name: config.CommonName},
ObjectMeta: metav1.ObjectMeta{Name: config.CSRName},
Spec: capi.CertificateSigningRequestSpec{
Request: csrData,
},
Expand Down Expand Up @@ -142,7 +146,7 @@ func (c *CertAgent) WaitForCertificate() (req *capi.CertificateSigningRequest, e

// implement the client GET request to the signer in a poll loop.
if err = wait.PollImmediate(interval, timeout, func() (bool, error) {
req, err = c.client.Get(c.config.CommonName, metav1.GetOptions{})
req, err = c.client.Get(c.config.CSRName, metav1.GetOptions{})
if err != nil {
glog.Errorf("unable to retrieve approved CSR: %v. Retrying.", err)
return false, nil
Expand Down

0 comments on commit d9ad2cb

Please sign in to comment.