Bladeren bron

Updating options to be Disable* and of type *bool

adantop 2 jaren geleden
bovenliggende
commit
bedb040659
3 gewijzigde bestanden met toevoegingen van 40 en 16 verwijderingen
  1. 23 5
      authority/provisioner/gcp.go
  2. 10 6
      authority/provisioner/gcp_test.go
  3. 7 5
      authority/provisioner/utils_test.go

+ 23 - 5
authority/provisioner/gcp.go

@@ -30,6 +30,12 @@ const gcpCertsURL = "https://www.googleapis.com/oauth2/v3/certs"
 // gcpIdentityURL is the base url for the identity document in GCP.
 // gcpIdentityURL is the base url for the identity document in GCP.
 const gcpIdentityURL = "http://metadata/computeMetadata/v1/instance/service-accounts/default/identity"
 const gcpIdentityURL = "http://metadata/computeMetadata/v1/instance/service-accounts/default/identity"
 
 
+// DefaultDisableSSHCAHost is the default value for SSH Host CA used when DisableSSHCAHost is not set
+var DefaultDisableSSHCAHost = false
+
+// DefaultDisableSSHCAUser is the default value for SSH User CA used when DisableSSHCAUser is not set
+var DefaultDisableSSHCAUser = true
+
 // gcpPayload extends jwt.Claims with custom GCP attributes.
 // gcpPayload extends jwt.Claims with custom GCP attributes.
 type gcpPayload struct {
 type gcpPayload struct {
 	jose.Claims
 	jose.Claims
@@ -89,8 +95,8 @@ type GCP struct {
 	ProjectIDs             []string `json:"projectIDs"`
 	ProjectIDs             []string `json:"projectIDs"`
 	DisableCustomSANs      bool     `json:"disableCustomSANs"`
 	DisableCustomSANs      bool     `json:"disableCustomSANs"`
 	DisableTrustOnFirstUse bool     `json:"disableTrustOnFirstUse"`
 	DisableTrustOnFirstUse bool     `json:"disableTrustOnFirstUse"`
-	EnableSSHCAUser        bool     `json:"enableSSHCAUser"`
-	DisableSSHCAHost       bool     `json:"disableSSHCAHost"`
+	DisableSSHCAUser       *bool    `json:"disableSSHCAUser,omitempty"`
+	DisableSSHCAHost       *bool    `json:"disableSSHCAHost,omitempty"`
 	InstanceAge            Duration `json:"instanceAge,omitempty"`
 	InstanceAge            Duration `json:"instanceAge,omitempty"`
 	Claims                 *Claims  `json:"claims,omitempty"`
 	Claims                 *Claims  `json:"claims,omitempty"`
 	Options                *Options `json:"options,omitempty"`
 	Options                *Options `json:"options,omitempty"`
@@ -201,6 +207,14 @@ func (p *GCP) GetIdentityToken(subject, caURL string) (string, error) {
 
 
 // Init validates and initializes the GCP provisioner.
 // Init validates and initializes the GCP provisioner.
 func (p *GCP) Init(config Config) (err error) {
 func (p *GCP) Init(config Config) (err error) {
+	if p.DisableSSHCAHost == nil {
+		p.DisableSSHCAHost = &DefaultDisableSSHCAHost
+	}
+
+	if p.DisableSSHCAUser == nil {
+		p.DisableSSHCAUser = &DefaultDisableSSHCAUser
+	}
+
 	switch {
 	switch {
 	case p.Type == "":
 	case p.Type == "":
 		return errors.New("provisioner type cannot be empty")
 		return errors.New("provisioner type cannot be empty")
@@ -479,10 +493,14 @@ func (p *GCP) genHostOptions(_ context.Context, claims *gcpPayload) (SignSSHOpti
 	return SignSSHOptions{CertType: SSHHostCert}, keyID, principals, sshutil.HostCert, sshutil.DefaultIIDTemplate
 	return SignSSHOptions{CertType: SSHHostCert}, keyID, principals, sshutil.HostCert, sshutil.DefaultIIDTemplate
 }
 }
 
 
+func FormatServiceAccountUsername(serviceAccountId string) string {
+	return fmt.Sprintf("sa_%v", serviceAccountId)
+}
+
 func (p *GCP) genUserOptions(_ context.Context, claims *gcpPayload) (SignSSHOptions, string, []string, sshutil.CertType, string) {
 func (p *GCP) genUserOptions(_ context.Context, claims *gcpPayload) (SignSSHOptions, string, []string, sshutil.CertType, string) {
 	keyID := claims.Email
 	keyID := claims.Email
 	principals := []string{
 	principals := []string{
-		fmt.Sprintf("sa_%v", claims.Subject),
+		FormatServiceAccountUsername(claims.Subject),
 		claims.Email,
 		claims.Email,
 	}
 	}
 
 
@@ -494,11 +512,11 @@ func (p *GCP) isUnauthorizedToIssueSSHCert(certType string) error {
 		return errs.Unauthorized("gcp.AuthorizeSSHSign; sshCA is disabled for gcp provisioner '%s'", p.GetName())
 		return errs.Unauthorized("gcp.AuthorizeSSHSign; sshCA is disabled for gcp provisioner '%s'", p.GetName())
 	}
 	}
 
 
-	if certType == SSHHostCert && p.DisableSSHCAHost {
+	if certType == SSHHostCert && *p.DisableSSHCAHost {
 		return errs.Unauthorized("gcp.AuthorizeSSHSign; sshCA for Hosts is disabled for gcp provisioner '%s'", p.GetName())
 		return errs.Unauthorized("gcp.AuthorizeSSHSign; sshCA for Hosts is disabled for gcp provisioner '%s'", p.GetName())
 	}
 	}
 
 
-	if certType == SSHUserCert && !p.EnableSSHCAUser {
+	if certType == SSHUserCert && *p.DisableSSHCAUser {
 		return errs.Unauthorized("gcp.AuthorizeSSHSign; sshCA for Users is disabled for gcp provisioner '%s'", p.GetName())
 		return errs.Unauthorized("gcp.AuthorizeSSHSign; sshCA for Users is disabled for gcp provisioner '%s'", p.GetName())
 	}
 	}
 
 

+ 10 - 6
authority/provisioner/gcp_test.go

@@ -213,11 +213,11 @@ func TestGCP_Init(t *testing.T) {
 				t.Errorf("GCP.Init() error = %v, wantErr %v", err, tt.wantErr)
 				t.Errorf("GCP.Init() error = %v, wantErr %v", err, tt.wantErr)
 			}
 			}
 
 
-			if p.EnableSSHCAUser {
-				t.Errorf("By default EnableSSHCAUser should be false")
+			if *p.DisableSSHCAUser != true {
+				t.Errorf("By default DisableSSHCAUser should be true")
 			}
 			}
 
 
-			if p.DisableSSHCAHost {
+			if *p.DisableSSHCAHost != false {
 				t.Errorf("By default DisableSSHCAHost should be false")
 				t.Errorf("By default DisableSSHCAHost should be false")
 			}
 			}
 		})
 		})
@@ -601,7 +601,9 @@ func TestGCP_AuthorizeSSHSign(t *testing.T) {
 	p1, err := generateGCP()
 	p1, err := generateGCP()
 	assert.FatalError(t, err)
 	assert.FatalError(t, err)
 	p1.DisableCustomSANs = true
 	p1.DisableCustomSANs = true
-	p1.EnableSSHCAUser = true
+	// enable ssh user CA
+	disableSSCAUser := false
+	p1.DisableSSHCAUser = &disableSSCAUser
 
 
 	p2, err := generateGCP()
 	p2, err := generateGCP()
 	assert.FatalError(t, err)
 	assert.FatalError(t, err)
@@ -617,7 +619,9 @@ func TestGCP_AuthorizeSSHSign(t *testing.T) {
 
 
 	p4, err := generateGCP()
 	p4, err := generateGCP()
 	assert.FatalError(t, err)
 	assert.FatalError(t, err)
-	p4.DisableSSHCAHost = true
+	// disable ssh host CA
+	disableSSCAHost := true
+	p4.DisableSSHCAHost = &disableSSCAHost
 
 
 	t1, err := generateGCPToken(p1.ServiceAccounts[0],
 	t1, err := generateGCPToken(p1.ServiceAccounts[0],
 		"https://accounts.google.com", p1.GetID(),
 		"https://accounts.google.com", p1.GetID(),
@@ -662,7 +666,7 @@ func TestGCP_AuthorizeSSHSign(t *testing.T) {
 		ValidAfter: NewTimeDuration(tm), ValidBefore: NewTimeDuration(tm.Add(hostDuration)),
 		ValidAfter: NewTimeDuration(tm), ValidBefore: NewTimeDuration(tm.Add(hostDuration)),
 	}
 	}
 	expectedUserOptions := &SignSSHOptions{
 	expectedUserOptions := &SignSSHOptions{
-		CertType: "user", Principals: []string{"foo", "foo@developer.gserviceaccount.com"},
+		CertType: "user", Principals: []string{FormatServiceAccountUsername(p1.ServiceAccounts[0]), "foo@developer.gserviceaccount.com"},
 		ValidAfter: NewTimeDuration(tm), ValidBefore: NewTimeDuration(tm.Add(p1.ctl.Claimer.DefaultUserSSHCertDuration())),
 		ValidAfter: NewTimeDuration(tm), ValidBefore: NewTimeDuration(tm.Add(p1.ctl.Claimer.DefaultUserSSHCertDuration())),
 	}
 	}
 
 

+ 7 - 5
authority/provisioner/utils_test.go

@@ -363,11 +363,13 @@ func generateGCP() (*GCP, error) {
 		return nil, err
 		return nil, err
 	}
 	}
 	p := &GCP{
 	p := &GCP{
-		Type:            "GCP",
-		Name:            name,
-		ServiceAccounts: []string{serviceAccount},
-		Claims:          &globalProvisionerClaims,
-		config:          newGCPConfig(),
+		Type:             "GCP",
+		Name:             name,
+		ServiceAccounts:  []string{serviceAccount},
+		Claims:           &globalProvisionerClaims,
+		DisableSSHCAHost: &DefaultDisableSSHCAHost,
+		DisableSSHCAUser: &DefaultDisableSSHCAUser,
+		config:           newGCPConfig(),
 		keyStore: &keyStore{
 		keyStore: &keyStore{
 			keySet: jose.JSONWebKeySet{Keys: []jose.JSONWebKey{*jwk}},
 			keySet: jose.JSONWebKeySet{Keys: []jose.JSONWebKey{*jwk}},
 			expiry: time.Now().Add(24 * time.Hour),
 			expiry: time.Now().Add(24 * time.Hour),