Skip to content
This repository was archived by the owner on Jan 11, 2023. It is now read-only.

Commit c0b88ca

Browse files
weinongjackfrancis
authored andcommitted
validate sp profile secret (#1187)
* validate sp profile secret * added a check in v20170701
1 parent 449889d commit c0b88ca

File tree

5 files changed

+89
-0
lines changed

5 files changed

+89
-0
lines changed

pkg/api/v20170131/.validate.go.swp

16 KB
Binary file not shown.

pkg/api/v20170131/validate.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,16 @@ func (a *Properties) Validate() error {
100100
return e
101101
}
102102

103+
if a.OrchestratorProfile.OrchestratorType == Kubernetes {
104+
if a.ServicePrincipalProfile == nil {
105+
return fmt.Errorf("ServicePrincipalProfile must be specified with Orchestrator %s", a.OrchestratorProfile.OrchestratorType)
106+
}
107+
108+
if len(a.ServicePrincipalProfile.Secret) == 0 {
109+
return fmt.Errorf("service principal client secret must be specified with Orchestrator %s", a.OrchestratorProfile.OrchestratorType)
110+
}
111+
}
112+
103113
for _, agentPoolProfile := range a.AgentPoolProfiles {
104114
if e := agentPoolProfile.Validate(a.OrchestratorProfile.OrchestratorType); e != nil {
105115
return e

pkg/api/v20170131/validate_test.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package v20170131
2+
3+
import "testing"
4+
5+
func Test_ServicePrincipalProfile_ValidateSecret(t *testing.T) {
6+
7+
t.Run("ServicePrincipalProfile is nil should fail", func(t *testing.T) {
8+
p := getK8sDefaultProperties()
9+
p.ServicePrincipalProfile = nil
10+
11+
if err := p.Validate(); err == nil {
12+
t.Errorf("should error %v", err)
13+
}
14+
})
15+
16+
t.Run("ServicePrincipalProfile with secret should pass", func(t *testing.T) {
17+
p := getK8sDefaultProperties()
18+
19+
if err := p.Validate(); err != nil {
20+
t.Errorf("should not error %v", err)
21+
}
22+
})
23+
24+
t.Run("ServicePrincipalProfile with missing secret should pass", func(t *testing.T) {
25+
p := getK8sDefaultProperties()
26+
p.ServicePrincipalProfile.Secret = ""
27+
28+
if err := p.Validate(); err == nil {
29+
t.Error("error should have occurred")
30+
}
31+
})
32+
33+
}
34+
35+
func getK8sDefaultProperties() *Properties {
36+
return &Properties{
37+
OrchestratorProfile: &OrchestratorProfile{
38+
OrchestratorType: Kubernetes,
39+
},
40+
MasterProfile: &MasterProfile{
41+
Count: 1,
42+
DNSPrefix: "foo",
43+
},
44+
AgentPoolProfiles: []*AgentPoolProfile{
45+
{
46+
Name: "agentpool",
47+
VMSize: "Standard_D2_v2",
48+
Count: 1,
49+
},
50+
},
51+
LinuxProfile: &LinuxProfile{
52+
AdminUsername: "azureuser",
53+
SSH: struct {
54+
PublicKeys []PublicKey `json:"publicKeys"`
55+
}{
56+
PublicKeys: []PublicKey{{
57+
KeyData: "publickeydata",
58+
}},
59+
},
60+
},
61+
ServicePrincipalProfile: &ServicePrincipalProfile{
62+
ClientID: "clientID",
63+
Secret: "clientSecret",
64+
},
65+
}
66+
}

pkg/api/v20170701/validate.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,10 @@ func (a *Properties) Validate() error {
133133
}
134134

135135
if a.OrchestratorProfile.OrchestratorType == Kubernetes {
136+
if a.ServicePrincipalProfile == nil {
137+
return fmt.Errorf("ServicePrincipalProfile must be specified with Orchestrator %s", a.OrchestratorProfile.OrchestratorType)
138+
}
139+
136140
if (len(a.ServicePrincipalProfile.Secret) == 0 && len(a.ServicePrincipalProfile.KeyvaultSecretRef) == 0) ||
137141
(len(a.ServicePrincipalProfile.Secret) != 0 && len(a.ServicePrincipalProfile.KeyvaultSecretRef) != 0) {
138142
return fmt.Errorf("either the service principal client secret or keyvault secret reference must be specified with Orchestrator %s", a.OrchestratorProfile.OrchestratorType)

pkg/api/v20170701/validate_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,15 @@ import "testing"
44

55
func Test_ServicePrincipalProfile_ValidateSecretOrKeyvaultSecretRef(t *testing.T) {
66

7+
t.Run("ServicePrincipalProfile is nil should fail", func(t *testing.T) {
8+
p := getK8sDefaultProperties()
9+
p.ServicePrincipalProfile = nil
10+
11+
if err := p.Validate(); err == nil {
12+
t.Errorf("should error %v", err)
13+
}
14+
})
15+
716
t.Run("ServicePrincipalProfile with secret should pass", func(t *testing.T) {
817
p := getK8sDefaultProperties()
918

0 commit comments

Comments
 (0)