certificates/authority/provisioner/azure_test.go

385 lines
11 KiB
Go
Raw Normal View History

package provisioner
import (
2019-05-08 19:47:45 +00:00
"crypto/sha256"
"crypto/x509"
2019-05-08 19:47:45 +00:00
"encoding/hex"
"fmt"
"net/http"
"net/http/httptest"
"strings"
"testing"
2019-05-08 19:47:45 +00:00
"time"
"github.com/smallstep/assert"
)
func TestAzure_Getters(t *testing.T) {
p, err := generateAzure()
assert.FatalError(t, err)
if got := p.GetID(); got != p.TenantID {
t.Errorf("Azure.GetID() = %v, want %v", got, p.TenantID)
}
if got := p.GetName(); got != p.Name {
t.Errorf("Azure.GetName() = %v, want %v", got, p.Name)
}
if got := p.GetType(); got != TypeAzure {
t.Errorf("Azure.GetType() = %v, want %v", got, TypeAzure)
}
kid, key, ok := p.GetEncryptedKey()
if kid != "" || key != "" || ok == true {
t.Errorf("Azure.GetEncryptedKey() = (%v, %v, %v), want (%v, %v, %v)",
kid, key, ok, "", "", false)
}
}
func TestAzure_GetTokenID(t *testing.T) {
2019-05-08 19:47:45 +00:00
p1, srv, err := generateAzureWithServer()
assert.FatalError(t, err)
defer srv.Close()
p2, err := generateAzure()
assert.FatalError(t, err)
p2.TenantID = p1.TenantID
p2.config = p1.config
p2.oidcConfig = p1.oidcConfig
p2.keyStore = p1.keyStore
p2.DisableTrustOnFirstUse = true
t1, err := p1.GetIdentityToken()
assert.FatalError(t, err)
t2, err := p2.GetIdentityToken()
assert.FatalError(t, err)
sum := sha256.Sum256([]byte("/subscriptions/subscriptionID/resourceGroups/resourceGroup/providers/Microsoft.Compute/virtualMachines/virtualMachine"))
w1 := strings.ToLower(hex.EncodeToString(sum[:]))
type args struct {
token string
}
tests := []struct {
name string
2019-05-08 19:47:45 +00:00
azure *Azure
args args
want string
wantErr bool
}{
2019-05-08 19:47:45 +00:00
{"ok", p1, args{t1}, w1, false},
{"ok no TOFU", p2, args{t2}, "the-jti", false},
{"fail token", p1, args{"bad-token"}, "", true},
{"fail claims", p1, args{"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.ey.fooo"}, "", true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
2019-05-08 19:47:45 +00:00
got, err := tt.azure.GetTokenID(tt.args.token)
if (err != nil) != tt.wantErr {
t.Errorf("Azure.GetTokenID() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("Azure.GetTokenID() = %v, want %v", got, tt.want)
}
})
}
}
2019-05-08 19:47:45 +00:00
func TestAzure_GetIdentityToken(t *testing.T) {
p1, err := generateAzure()
assert.FatalError(t, err)
t1, err := generateAzureToken("subject", p1.oidcConfig.Issuer, azureDefaultAudience,
p1.TenantID, "subscriptionID", "resourceGroup", "virtualMachine",
time.Now(), &p1.keyStore.keySet.Keys[0])
assert.FatalError(t, err)
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/bad-request":
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
case "/bad-json":
w.Write([]byte(t1))
default:
w.Header().Add("Content-Type", "application/json")
w.Write([]byte(fmt.Sprintf(`{"access_token":"%s"}`, t1)))
}
}))
defer srv.Close()
tests := []struct {
name string
azure *Azure
identityTokenURL string
want string
wantErr bool
}{
{"ok", p1, srv.URL, t1, false},
{"fail request", p1, srv.URL + "/bad-request", "", true},
{"fail unmarshal", p1, srv.URL + "/bad-json", "", true},
{"fail url", p1, "://ca.smallstep.com", "", true},
{"fail connect", p1, "foobarzar", "", true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.azure.config.identityTokenURL = tt.identityTokenURL
got, err := tt.azure.GetIdentityToken()
if (err != nil) != tt.wantErr {
t.Errorf("Azure.GetIdentityToken() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("Azure.GetIdentityToken() = %v, want %v", got, tt.want)
}
})
}
}
func TestAzure_Init(t *testing.T) {
2019-05-08 19:47:45 +00:00
p1, srv, err := generateAzureWithServer()
assert.FatalError(t, err)
defer srv.Close()
config := Config{
Claims: globalProvisionerClaims,
}
badClaims := &Claims{
DefaultTLSDur: &Duration{0},
}
2019-05-08 19:47:45 +00:00
badDiscoveryURL := &azureConfig{
oidcDiscoveryURL: srv.URL + "/error",
identityTokenURL: p1.config.identityTokenURL,
}
badJWKURL := &azureConfig{
oidcDiscoveryURL: srv.URL + "/openid-configuration-fail-jwk",
identityTokenURL: p1.config.identityTokenURL,
}
badAzureConfig := &azureConfig{
oidcDiscoveryURL: srv.URL + "/openid-configuration-no-issuer",
identityTokenURL: p1.config.identityTokenURL,
}
type fields struct {
2019-05-08 19:47:45 +00:00
Type string
Name string
TenantID string
Claims *Claims
config *azureConfig
}
type args struct {
config Config
}
tests := []struct {
name string
fields fields
args args
wantErr bool
}{
2019-05-08 19:47:45 +00:00
{"ok", fields{p1.Type, p1.Name, p1.TenantID, nil, p1.config}, args{config}, false},
{"ok with config", fields{p1.Type, p1.Name, p1.TenantID, nil, p1.config}, args{config}, false},
{"fail type", fields{"", p1.Name, p1.TenantID, nil, p1.config}, args{config}, true},
{"fail name", fields{p1.Type, "", p1.TenantID, nil, p1.config}, args{config}, true},
{"fail tenant id", fields{p1.Type, p1.Name, "", nil, p1.config}, args{config}, true},
{"fail claims", fields{p1.Type, p1.Name, p1.TenantID, badClaims, p1.config}, args{config}, true},
{"fail discovery URL", fields{p1.Type, p1.Name, p1.TenantID, nil, badDiscoveryURL}, args{config}, true},
{"fail JWK URL", fields{p1.Type, p1.Name, p1.TenantID, nil, badJWKURL}, args{config}, true},
{"fail config Validate", fields{p1.Type, p1.Name, p1.TenantID, nil, badAzureConfig}, args{config}, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
p := &Azure{
2019-05-08 19:47:45 +00:00
Type: tt.fields.Type,
Name: tt.fields.Name,
TenantID: tt.fields.TenantID,
Claims: tt.fields.Claims,
config: tt.fields.config,
}
if err := p.Init(tt.args.config); (err != nil) != tt.wantErr {
t.Errorf("Azure.Init() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestAzure_AuthorizeSign(t *testing.T) {
2019-05-08 19:47:45 +00:00
p1, srv, err := generateAzureWithServer()
assert.FatalError(t, err)
defer srv.Close()
p2, err := generateAzure()
assert.FatalError(t, err)
p2.TenantID = p1.TenantID
p2.ResourceGroups = []string{"resourceGroup"}
2019-05-08 19:47:45 +00:00
p2.config = p1.config
p2.oidcConfig = p1.oidcConfig
p2.keyStore = p1.keyStore
p2.DisableCustomSANs = true
p3, err := generateAzure()
assert.FatalError(t, err)
p3.config = p1.config
p3.oidcConfig = p1.oidcConfig
p3.keyStore = p1.keyStore
p4, err := generateAzure()
assert.FatalError(t, err)
p4.TenantID = p1.TenantID
p4.ResourceGroups = []string{"foobarzar"}
2019-05-08 19:47:45 +00:00
p4.config = p1.config
p4.oidcConfig = p1.oidcConfig
p4.keyStore = p1.keyStore
badKey, err := generateJSONWebKey()
assert.FatalError(t, err)
t1, err := p1.GetIdentityToken()
assert.FatalError(t, err)
t2, err := p2.GetIdentityToken()
assert.FatalError(t, err)
t3, err := p3.GetIdentityToken()
assert.FatalError(t, err)
t4, err := p4.GetIdentityToken()
assert.FatalError(t, err)
t11, err := generateAzureToken("subject", p1.oidcConfig.Issuer, azureDefaultAudience,
p1.TenantID, "subscriptionID", "resourceGroup", "virtualMachine",
time.Now(), &p1.keyStore.keySet.Keys[0])
assert.FatalError(t, err)
failIssuer, err := generateAzureToken("subject", "bad-issuer", azureDefaultAudience,
p1.TenantID, "subscriptionID", "resourceGroup", "virtualMachine",
time.Now(), &p1.keyStore.keySet.Keys[0])
assert.FatalError(t, err)
failAudience, err := generateAzureToken("subject", p1.oidcConfig.Issuer, "bad-audience",
p1.TenantID, "subscriptionID", "resourceGroup", "virtualMachine",
time.Now(), &p1.keyStore.keySet.Keys[0])
assert.FatalError(t, err)
failExp, err := generateAzureToken("subject", p1.oidcConfig.Issuer, azureDefaultAudience,
p1.TenantID, "subscriptionID", "resourceGroup", "virtualMachine",
time.Now().Add(-360*time.Second), &p1.keyStore.keySet.Keys[0])
assert.FatalError(t, err)
failNbf, err := generateAzureToken("subject", p1.oidcConfig.Issuer, azureDefaultAudience,
p1.TenantID, "subscriptionID", "resourceGroup", "virtualMachine",
time.Now().Add(360*time.Second), &p1.keyStore.keySet.Keys[0])
assert.FatalError(t, err)
failKey, err := generateAzureToken("subject", p1.oidcConfig.Issuer, azureDefaultAudience,
p1.TenantID, "subscriptionID", "resourceGroup", "virtualMachine",
time.Now(), badKey)
assert.FatalError(t, err)
type args struct {
token string
}
tests := []struct {
name string
2019-05-08 19:47:45 +00:00
azure *Azure
args args
2019-05-08 19:47:45 +00:00
wantLen int
wantErr bool
}{
2019-05-08 19:47:45 +00:00
{"ok", p1, args{t1}, 4, false},
{"ok", p2, args{t2}, 5, false},
{"ok", p1, args{t11}, 4, false},
{"fail tenant", p3, args{t3}, 0, true},
{"fail resource group", p4, args{t4}, 0, true},
2019-05-08 19:47:45 +00:00
{"fail token", p1, args{"token"}, 0, true},
{"fail issuer", p1, args{failIssuer}, 0, true},
{"fail audience", p1, args{failAudience}, 0, true},
{"fail exp", p1, args{failExp}, 0, true},
{"fail nbf", p1, args{failNbf}, 0, true},
{"fail key", p1, args{failKey}, 0, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
2019-05-08 19:47:45 +00:00
got, err := tt.azure.AuthorizeSign(tt.args.token)
if (err != nil) != tt.wantErr {
t.Errorf("Azure.AuthorizeSign() error = %v, wantErr %v", err, tt.wantErr)
return
}
2019-05-08 19:47:45 +00:00
assert.Len(t, tt.wantLen, got)
})
}
}
func TestAzure_AuthorizeRenewal(t *testing.T) {
p1, err := generateAzure()
assert.FatalError(t, err)
p2, err := generateAzure()
assert.FatalError(t, err)
// disable renewal
disable := true
p2.Claims = &Claims{DisableRenewal: &disable}
p2.claimer, err = NewClaimer(p2.Claims, globalProvisionerClaims)
assert.FatalError(t, err)
type args struct {
cert *x509.Certificate
}
tests := []struct {
name string
azure *Azure
args args
wantErr bool
}{
{"ok", p1, args{nil}, false},
{"fail", p2, args{nil}, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err := tt.azure.AuthorizeRenewal(tt.args.cert); (err != nil) != tt.wantErr {
t.Errorf("Azure.AuthorizeRenewal() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
func TestAzure_AuthorizeRevoke(t *testing.T) {
2019-05-08 19:47:45 +00:00
az, srv, err := generateAzureWithServer()
assert.FatalError(t, err)
defer srv.Close()
token, err := az.GetIdentityToken()
assert.FatalError(t, err)
type args struct {
token string
}
tests := []struct {
name string
2019-05-08 19:47:45 +00:00
azure *Azure
args args
wantErr bool
}{
2019-05-08 19:47:45 +00:00
{"ok token", az, args{token}, true}, // revoke is disabled
{"bad token", az, args{"bad token"}, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
2019-05-08 19:47:45 +00:00
if err := tt.azure.AuthorizeRevoke(tt.args.token); (err != nil) != tt.wantErr {
t.Errorf("Azure.AuthorizeRevoke() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}
2019-05-08 19:47:45 +00:00
func TestAzure_assertConfig(t *testing.T) {
p1, err := generateAzure()
assert.FatalError(t, err)
p2, err := generateAzure()
assert.FatalError(t, err)
p2.config = nil
tests := []struct {
name string
azure *Azure
}{
{"ok with config", p1},
{"ok no config", p2},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.azure.assertConfig()
})
}
}