2019-04-18 23:01:30 +00:00
|
|
|
package provisioner
|
|
|
|
|
|
|
|
import (
|
2019-07-30 01:24:34 +00:00
|
|
|
"context"
|
2019-08-01 01:22:21 +00:00
|
|
|
"crypto"
|
2019-09-11 00:04:13 +00:00
|
|
|
"crypto/rand"
|
|
|
|
"crypto/rsa"
|
2019-04-18 23:01:30 +00:00
|
|
|
"crypto/sha256"
|
|
|
|
"crypto/x509"
|
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2019-12-20 21:30:05 +00:00
|
|
|
"github.com/pkg/errors"
|
2019-04-18 23:01:30 +00:00
|
|
|
"github.com/smallstep/assert"
|
2019-12-20 21:30:05 +00:00
|
|
|
"github.com/smallstep/certificates/errs"
|
|
|
|
"github.com/smallstep/cli/jose"
|
2019-04-18 23:01:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestGCP_Getters(t *testing.T) {
|
|
|
|
p, err := generateGCP()
|
|
|
|
assert.FatalError(t, err)
|
2019-06-04 00:19:44 +00:00
|
|
|
id := "gcp/" + p.Name
|
|
|
|
if got := p.GetID(); got != id {
|
|
|
|
t.Errorf("GCP.GetID() = %v, want %v", got, id)
|
2019-04-18 23:01:30 +00:00
|
|
|
}
|
|
|
|
if got := p.GetName(); got != p.Name {
|
|
|
|
t.Errorf("GCP.GetName() = %v, want %v", got, p.Name)
|
|
|
|
}
|
|
|
|
if got := p.GetType(); got != TypeGCP {
|
|
|
|
t.Errorf("GCP.GetType() = %v, want %v", got, TypeGCP)
|
|
|
|
}
|
|
|
|
kid, key, ok := p.GetEncryptedKey()
|
|
|
|
if kid != "" || key != "" || ok == true {
|
|
|
|
t.Errorf("GCP.GetEncryptedKey() = (%v, %v, %v), want (%v, %v, %v)",
|
|
|
|
kid, key, ok, "", "", false)
|
|
|
|
}
|
2019-06-04 00:19:44 +00:00
|
|
|
|
|
|
|
aud := "https://ca.smallstep.com/1.0/sign#" + url.QueryEscape(id)
|
|
|
|
expected := fmt.Sprintf("http://metadata/computeMetadata/v1/instance/service-accounts/default/identity?audience=%s&format=full&licenses=FALSE", url.QueryEscape(aud))
|
|
|
|
if got := p.GetIdentityURL(aud); got != expected {
|
2019-04-18 23:01:30 +00:00
|
|
|
t.Errorf("GCP.GetIdentityURL() = %v, want %v", got, expected)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGCP_GetTokenID(t *testing.T) {
|
|
|
|
p1, err := generateGCP()
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
p1.Name = "name"
|
|
|
|
|
2019-04-24 20:05:46 +00:00
|
|
|
p2, err := generateGCP()
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
p2.DisableTrustOnFirstUse = true
|
|
|
|
|
2019-04-18 23:01:30 +00:00
|
|
|
now := time.Now()
|
|
|
|
t1, err := generateGCPToken(p1.ServiceAccounts[0],
|
2019-06-04 00:19:44 +00:00
|
|
|
"https://accounts.google.com", "gcp/name",
|
2019-04-18 23:01:30 +00:00
|
|
|
"instance-id", "instance-name", "project-id", "zone",
|
|
|
|
now, &p1.keyStore.keySet.Keys[0])
|
|
|
|
assert.FatalError(t, err)
|
2019-04-24 20:05:46 +00:00
|
|
|
t2, err := generateGCPToken(p2.ServiceAccounts[0],
|
|
|
|
"https://accounts.google.com", p2.GetID(),
|
|
|
|
"instance-id", "instance-name", "project-id", "zone",
|
|
|
|
now, &p2.keyStore.keySet.Keys[0])
|
|
|
|
assert.FatalError(t, err)
|
2019-04-18 23:01:30 +00:00
|
|
|
|
2019-06-04 00:19:44 +00:00
|
|
|
sum := sha256.Sum256([]byte("gcp/name.instance-id"))
|
2019-04-24 20:05:46 +00:00
|
|
|
want1 := strings.ToLower(hex.EncodeToString(sum[:]))
|
|
|
|
sum = sha256.Sum256([]byte(t2))
|
|
|
|
want2 := strings.ToLower(hex.EncodeToString(sum[:]))
|
2019-04-18 23:01:30 +00:00
|
|
|
|
|
|
|
type args struct {
|
|
|
|
token string
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
gcp *GCP
|
|
|
|
args args
|
|
|
|
want string
|
|
|
|
wantErr bool
|
|
|
|
}{
|
2019-04-24 20:05:46 +00:00
|
|
|
{"ok", p1, args{t1}, want1, false},
|
|
|
|
{"ok", p2, args{t2}, want2, false},
|
2019-04-18 23:01:30 +00:00
|
|
|
{"fail token", p1, args{"token"}, "", true},
|
|
|
|
{"fail claims", p1, args{"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.ey.fooo"}, "", true},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
got, err := tt.gcp.GetTokenID(tt.args.token)
|
|
|
|
if (err != nil) != tt.wantErr {
|
|
|
|
t.Errorf("GCP.GetTokenID() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if got != tt.want {
|
|
|
|
t.Errorf("GCP.GetTokenID() = %v, want %v", got, tt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGCP_GetIdentityToken(t *testing.T) {
|
|
|
|
p1, err := generateGCP()
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
|
|
|
|
t1, err := generateGCPToken(p1.ServiceAccounts[0],
|
|
|
|
"https://accounts.google.com", p1.GetID(),
|
|
|
|
"instance-id", "instance-name", "project-id", "zone",
|
|
|
|
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)
|
|
|
|
default:
|
|
|
|
w.Write([]byte(t1))
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
defer srv.Close()
|
|
|
|
|
2019-06-04 00:19:44 +00:00
|
|
|
type args struct {
|
2019-07-15 22:52:36 +00:00
|
|
|
subject string
|
|
|
|
caURL string
|
2019-06-04 00:19:44 +00:00
|
|
|
}
|
2019-04-18 23:01:30 +00:00
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
gcp *GCP
|
2019-06-04 00:19:44 +00:00
|
|
|
args args
|
2019-04-18 23:01:30 +00:00
|
|
|
identityURL string
|
|
|
|
want string
|
|
|
|
wantErr bool
|
|
|
|
}{
|
2019-07-15 22:52:36 +00:00
|
|
|
{"ok", p1, args{"subject", "https://ca"}, srv.URL, t1, false},
|
|
|
|
{"fail ca url", p1, args{"subject", "://ca"}, srv.URL, "", true},
|
|
|
|
{"fail request", p1, args{"subject", "https://ca"}, srv.URL + "/bad-request", "", true},
|
|
|
|
{"fail url", p1, args{"subject", "https://ca"}, "://ca.smallstep.com", "", true},
|
|
|
|
{"fail connect", p1, args{"subject", "https://ca"}, "foobarzar", "", true},
|
2019-04-18 23:01:30 +00:00
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2019-04-23 21:33:36 +00:00
|
|
|
tt.gcp.config.IdentityURL = tt.identityURL
|
2019-07-15 22:52:36 +00:00
|
|
|
got, err := tt.gcp.GetIdentityToken(tt.args.subject, tt.args.caURL)
|
2019-04-19 17:44:11 +00:00
|
|
|
t.Log(err)
|
2019-04-18 23:01:30 +00:00
|
|
|
if (err != nil) != tt.wantErr {
|
|
|
|
t.Errorf("GCP.GetIdentityToken() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if got != tt.want {
|
|
|
|
t.Errorf("GCP.GetIdentityToken() = %v, want %v", got, tt.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGCP_Init(t *testing.T) {
|
|
|
|
srv := generateJWKServer(2)
|
|
|
|
defer srv.Close()
|
|
|
|
|
|
|
|
config := Config{
|
|
|
|
Claims: globalProvisionerClaims,
|
|
|
|
}
|
|
|
|
badClaims := &Claims{
|
|
|
|
DefaultTLSDur: &Duration{0},
|
|
|
|
}
|
2019-06-04 22:57:15 +00:00
|
|
|
zero := Duration{Duration: 0}
|
2019-04-18 23:01:30 +00:00
|
|
|
type fields struct {
|
|
|
|
Type string
|
|
|
|
Name string
|
|
|
|
ServiceAccounts []string
|
2019-06-04 22:57:15 +00:00
|
|
|
InstanceAge Duration
|
2019-04-18 23:01:30 +00:00
|
|
|
Claims *Claims
|
|
|
|
}
|
|
|
|
type args struct {
|
|
|
|
config Config
|
|
|
|
certsURL string
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
fields fields
|
|
|
|
args args
|
|
|
|
wantErr bool
|
|
|
|
}{
|
2019-06-04 22:57:15 +00:00
|
|
|
{"ok", fields{"GCP", "name", nil, zero, nil}, args{config, srv.URL}, false},
|
|
|
|
{"ok", fields{"GCP", "name", []string{"service-account"}, zero, nil}, args{config, srv.URL}, false},
|
|
|
|
{"ok", fields{"GCP", "name", []string{"service-account"}, Duration{Duration: 1 * time.Minute}, nil}, args{config, srv.URL}, false},
|
|
|
|
{"bad type", fields{"", "name", nil, zero, nil}, args{config, srv.URL}, true},
|
|
|
|
{"bad name", fields{"GCP", "", nil, zero, nil}, args{config, srv.URL}, true},
|
|
|
|
{"bad duration", fields{"GCP", "name", nil, Duration{Duration: -1 * time.Minute}, nil}, args{config, srv.URL}, true},
|
|
|
|
{"bad claims", fields{"GCP", "name", nil, zero, badClaims}, args{config, srv.URL}, true},
|
|
|
|
{"bad certs", fields{"GCP", "name", nil, zero, nil}, args{config, srv.URL + "/error"}, true},
|
2019-04-18 23:01:30 +00:00
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
p := &GCP{
|
|
|
|
Type: tt.fields.Type,
|
|
|
|
Name: tt.fields.Name,
|
|
|
|
ServiceAccounts: tt.fields.ServiceAccounts,
|
2019-06-04 22:57:15 +00:00
|
|
|
InstanceAge: tt.fields.InstanceAge,
|
2019-04-18 23:01:30 +00:00
|
|
|
Claims: tt.fields.Claims,
|
2019-04-23 21:33:36 +00:00
|
|
|
config: &gcpConfig{
|
|
|
|
CertsURL: tt.args.certsURL,
|
|
|
|
IdentityURL: gcpIdentityURL,
|
|
|
|
},
|
2019-04-18 23:01:30 +00:00
|
|
|
}
|
|
|
|
if err := p.Init(tt.args.config); (err != nil) != tt.wantErr {
|
|
|
|
t.Errorf("GCP.Init() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-20 21:30:05 +00:00
|
|
|
func TestGCP_authorizeToken(t *testing.T) {
|
|
|
|
type test struct {
|
|
|
|
p *GCP
|
|
|
|
token string
|
|
|
|
err error
|
|
|
|
code int
|
|
|
|
}
|
|
|
|
tests := map[string]func(*testing.T) test{
|
|
|
|
"fail/bad-token": func(t *testing.T) test {
|
|
|
|
p, err := generateGCP()
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
return test{
|
|
|
|
p: p,
|
|
|
|
token: "foo",
|
|
|
|
code: http.StatusUnauthorized,
|
|
|
|
err: errors.New("gcp.authorizeToken; error parsing gcp token"),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"fail/cannot-validate-sig": func(t *testing.T) test {
|
|
|
|
p, err := generateGCP()
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
jwk, err := jose.GenerateJWK("EC", "P-256", "ES256", "sig", "", 0)
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
tok, err := generateGCPToken(p.ServiceAccounts[0],
|
|
|
|
"https://accounts.google.com", p.GetID(),
|
|
|
|
"instance-id", "instance-name", "project-id", "zone",
|
|
|
|
time.Now(), jwk)
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
return test{
|
|
|
|
p: p,
|
|
|
|
token: tok,
|
|
|
|
code: http.StatusUnauthorized,
|
|
|
|
err: errors.New("gcp.authorizeToken; failed to validate gcp token payload - cannot find key for kid "),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"fail/invalid-issuer": func(t *testing.T) test {
|
|
|
|
p, err := generateGCP()
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
tok, err := generateGCPToken(p.ServiceAccounts[0],
|
|
|
|
"https://foo.bar.zap", p.GetID(),
|
|
|
|
"instance-id", "instance-name", "project-id", "zone",
|
|
|
|
time.Now(), &p.keyStore.keySet.Keys[0])
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
return test{
|
|
|
|
p: p,
|
|
|
|
token: tok,
|
|
|
|
code: http.StatusUnauthorized,
|
|
|
|
err: errors.New("gcp.authorizeToken; invalid gcp token payload"),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"fail/invalid-serviceAccount": func(t *testing.T) test {
|
|
|
|
p, err := generateGCP()
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
tok, err := generateGCPToken("foo",
|
|
|
|
"https://accounts.google.com", p.GetID(),
|
|
|
|
"instance-id", "instance-name", "project-id", "zone",
|
|
|
|
time.Now(), &p.keyStore.keySet.Keys[0])
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
return test{
|
|
|
|
p: p,
|
|
|
|
token: tok,
|
|
|
|
code: http.StatusUnauthorized,
|
|
|
|
err: errors.New("gcp.authorizeToken; invalid gcp token - invalid subject claim"),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"fail/invalid-projectID": func(t *testing.T) test {
|
|
|
|
p, err := generateGCP()
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
p.ProjectIDs = []string{"foo", "bar"}
|
|
|
|
tok, err := generateGCPToken(p.ServiceAccounts[0],
|
|
|
|
"https://accounts.google.com", p.GetID(),
|
|
|
|
"instance-id", "instance-name", "project-id", "zone",
|
|
|
|
time.Now(), &p.keyStore.keySet.Keys[0])
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
return test{
|
|
|
|
p: p,
|
|
|
|
token: tok,
|
|
|
|
code: http.StatusUnauthorized,
|
|
|
|
err: errors.New("gcp.authorizeToken; invalid gcp token - invalid project id"),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"fail/instance-age": func(t *testing.T) test {
|
|
|
|
p, err := generateGCP()
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
p.InstanceAge = Duration{1 * time.Minute}
|
|
|
|
tok, err := generateGCPToken(p.ServiceAccounts[0],
|
|
|
|
"https://accounts.google.com", p.GetID(),
|
|
|
|
"instance-id", "instance-name", "project-id", "zone",
|
|
|
|
time.Now().Add(-1*time.Minute), &p.keyStore.keySet.Keys[0])
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
return test{
|
|
|
|
p: p,
|
|
|
|
token: tok,
|
|
|
|
code: http.StatusUnauthorized,
|
|
|
|
err: errors.New("gcp.authorizeToken; token google.compute_engine.instance_creation_timestamp is too old"),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"fail/empty-instance-id": func(t *testing.T) test {
|
|
|
|
p, err := generateGCP()
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
tok, err := generateGCPToken(p.ServiceAccounts[0],
|
|
|
|
"https://accounts.google.com", p.GetID(),
|
|
|
|
"", "instance-name", "project-id", "zone",
|
|
|
|
time.Now(), &p.keyStore.keySet.Keys[0])
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
return test{
|
|
|
|
p: p,
|
|
|
|
token: tok,
|
|
|
|
code: http.StatusUnauthorized,
|
|
|
|
err: errors.New("gcp.authorizeToken; gcp token google.compute_engine.instance_id cannot be empty"),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"fail/empty-instance-name": func(t *testing.T) test {
|
|
|
|
p, err := generateGCP()
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
tok, err := generateGCPToken(p.ServiceAccounts[0],
|
|
|
|
"https://accounts.google.com", p.GetID(),
|
|
|
|
"instance-id", "", "project-id", "zone",
|
|
|
|
time.Now(), &p.keyStore.keySet.Keys[0])
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
return test{
|
|
|
|
p: p,
|
|
|
|
token: tok,
|
|
|
|
code: http.StatusUnauthorized,
|
|
|
|
err: errors.New("gcp.authorizeToken; gcp token google.compute_engine.instance_name cannot be empty"),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"fail/empty-project-id": func(t *testing.T) test {
|
|
|
|
p, err := generateGCP()
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
tok, err := generateGCPToken(p.ServiceAccounts[0],
|
|
|
|
"https://accounts.google.com", p.GetID(),
|
|
|
|
"instance-id", "instance-name", "", "zone",
|
|
|
|
time.Now(), &p.keyStore.keySet.Keys[0])
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
return test{
|
|
|
|
p: p,
|
|
|
|
token: tok,
|
|
|
|
code: http.StatusUnauthorized,
|
|
|
|
err: errors.New("gcp.authorizeToken; gcp token google.compute_engine.project_id cannot be empty"),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"fail/empty-zone": func(t *testing.T) test {
|
|
|
|
p, err := generateGCP()
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
tok, err := generateGCPToken(p.ServiceAccounts[0],
|
|
|
|
"https://accounts.google.com", p.GetID(),
|
|
|
|
"instance-id", "instance-name", "project-id", "",
|
|
|
|
time.Now(), &p.keyStore.keySet.Keys[0])
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
return test{
|
|
|
|
p: p,
|
|
|
|
token: tok,
|
|
|
|
code: http.StatusUnauthorized,
|
|
|
|
err: errors.New("gcp.authorizeToken; gcp token google.compute_engine.zone cannot be empty"),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"ok": func(t *testing.T) test {
|
|
|
|
p, err := generateGCP()
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
tok, err := generateGCPToken(p.ServiceAccounts[0],
|
|
|
|
"https://accounts.google.com", p.GetID(),
|
|
|
|
"instance-id", "instance-name", "project-id", "zone",
|
|
|
|
time.Now(), &p.keyStore.keySet.Keys[0])
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
return test{
|
|
|
|
p: p,
|
|
|
|
token: tok,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for name, tt := range tests {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
tc := tt(t)
|
|
|
|
if claims, err := tc.p.authorizeToken(tc.token); err != nil {
|
|
|
|
if assert.NotNil(t, tc.err) {
|
|
|
|
sc, ok := err.(errs.StatusCoder)
|
|
|
|
assert.Fatal(t, ok, "error does not implement StatusCoder interface")
|
|
|
|
assert.Equals(t, sc.StatusCode(), tc.code)
|
|
|
|
assert.HasPrefix(t, err.Error(), tc.err.Error())
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if assert.Nil(t, tc.err) && assert.NotNil(t, claims) {
|
|
|
|
assert.Equals(t, claims.Subject, tc.p.ServiceAccounts[0])
|
|
|
|
assert.Equals(t, claims.Issuer, "https://accounts.google.com")
|
|
|
|
assert.NotNil(t, claims.Google)
|
|
|
|
|
|
|
|
aud, err := generateSignAudience("https://ca.smallstep.com", tc.p.GetID())
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
assert.Equals(t, claims.Audience[0], aud)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-18 23:01:30 +00:00
|
|
|
func TestGCP_AuthorizeSign(t *testing.T) {
|
|
|
|
p1, err := generateGCP()
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
|
2019-04-24 20:05:46 +00:00
|
|
|
p2, err := generateGCP()
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
p2.DisableCustomSANs = true
|
|
|
|
|
2019-06-03 18:56:42 +00:00
|
|
|
p3, err := generateGCP()
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
p3.ProjectIDs = []string{"other-project-id"}
|
2019-06-04 00:28:39 +00:00
|
|
|
p3.ServiceAccounts = []string{"foo@developer.gserviceaccount.com"}
|
2019-06-04 22:57:15 +00:00
|
|
|
p3.InstanceAge = Duration{1 * time.Minute}
|
2019-06-03 18:56:42 +00:00
|
|
|
|
2019-04-18 23:01:30 +00:00
|
|
|
aKey, err := generateJSONWebKey()
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
|
|
|
|
t1, err := generateGCPToken(p1.ServiceAccounts[0],
|
|
|
|
"https://accounts.google.com", p1.GetID(),
|
|
|
|
"instance-id", "instance-name", "project-id", "zone",
|
|
|
|
time.Now(), &p1.keyStore.keySet.Keys[0])
|
|
|
|
assert.FatalError(t, err)
|
2019-04-24 20:05:46 +00:00
|
|
|
t2, err := generateGCPToken(p2.ServiceAccounts[0],
|
|
|
|
"https://accounts.google.com", p2.GetID(),
|
|
|
|
"instance-id", "instance-name", "project-id", "zone",
|
|
|
|
time.Now(), &p2.keyStore.keySet.Keys[0])
|
|
|
|
assert.FatalError(t, err)
|
2019-06-03 18:56:42 +00:00
|
|
|
t3, err := generateGCPToken(p3.ServiceAccounts[0],
|
|
|
|
"https://accounts.google.com", p3.GetID(),
|
|
|
|
"instance-id", "instance-name", "other-project-id", "zone",
|
|
|
|
time.Now(), &p3.keyStore.keySet.Keys[0])
|
|
|
|
assert.FatalError(t, err)
|
2019-04-24 20:05:46 +00:00
|
|
|
|
2019-04-18 23:01:30 +00:00
|
|
|
failKey, err := generateGCPToken(p1.ServiceAccounts[0],
|
|
|
|
"https://accounts.google.com", p1.GetID(),
|
|
|
|
"instance-id", "instance-name", "project-id", "zone",
|
|
|
|
time.Now(), aKey)
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
failIss, err := generateGCPToken(p1.ServiceAccounts[0],
|
|
|
|
"https://foo.bar.zar", p1.GetID(),
|
|
|
|
"instance-id", "instance-name", "project-id", "zone",
|
|
|
|
time.Now(), &p1.keyStore.keySet.Keys[0])
|
2019-04-24 21:59:01 +00:00
|
|
|
assert.FatalError(t, err)
|
2019-04-18 23:01:30 +00:00
|
|
|
failAud, err := generateGCPToken(p1.ServiceAccounts[0],
|
|
|
|
"https://accounts.google.com", "gcp:foo",
|
|
|
|
"instance-id", "instance-name", "project-id", "zone",
|
|
|
|
time.Now(), &p1.keyStore.keySet.Keys[0])
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
failExp, err := generateGCPToken(p1.ServiceAccounts[0],
|
|
|
|
"https://accounts.google.com", p1.GetID(),
|
|
|
|
"instance-id", "instance-name", "project-id", "zone",
|
|
|
|
time.Now().Add(-360*time.Second), &p1.keyStore.keySet.Keys[0])
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
failNbf, err := generateGCPToken(p1.ServiceAccounts[0],
|
|
|
|
"https://accounts.google.com", p1.GetID(),
|
|
|
|
"instance-id", "instance-name", "project-id", "zone",
|
|
|
|
time.Now().Add(360*time.Second), &p1.keyStore.keySet.Keys[0])
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
failServiceAccount, err := generateGCPToken("foo",
|
|
|
|
"https://accounts.google.com", p1.GetID(),
|
|
|
|
"instance-id", "instance-name", "project-id", "zone",
|
|
|
|
time.Now(), &p1.keyStore.keySet.Keys[0])
|
|
|
|
assert.FatalError(t, err)
|
2019-06-03 18:56:42 +00:00
|
|
|
failInvalidProjectID, err := generateGCPToken(p3.ServiceAccounts[0],
|
|
|
|
"https://accounts.google.com", p3.GetID(),
|
|
|
|
"instance-id", "instance-name", "project-id", "zone",
|
|
|
|
time.Now(), &p3.keyStore.keySet.Keys[0])
|
|
|
|
assert.FatalError(t, err)
|
2019-06-04 22:57:15 +00:00
|
|
|
failInvalidInstanceAge, err := generateGCPToken(p3.ServiceAccounts[0],
|
|
|
|
"https://accounts.google.com", p3.GetID(),
|
|
|
|
"instance-id", "instance-name", "other-project-id", "zone",
|
|
|
|
time.Now().Add(-1*time.Minute), &p3.keyStore.keySet.Keys[0])
|
|
|
|
assert.FatalError(t, err)
|
2019-04-18 23:01:30 +00:00
|
|
|
failInstanceID, err := generateGCPToken(p1.ServiceAccounts[0],
|
|
|
|
"https://accounts.google.com", p1.GetID(),
|
|
|
|
"", "instance-name", "project-id", "zone",
|
|
|
|
time.Now(), &p1.keyStore.keySet.Keys[0])
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
failInstanceName, err := generateGCPToken(p1.ServiceAccounts[0],
|
|
|
|
"https://accounts.google.com", p1.GetID(),
|
|
|
|
"instance-id", "", "project-id", "zone",
|
|
|
|
time.Now(), &p1.keyStore.keySet.Keys[0])
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
failProjectID, err := generateGCPToken(p1.ServiceAccounts[0],
|
|
|
|
"https://accounts.google.com", p1.GetID(),
|
|
|
|
"instance-id", "instance-name", "", "zone",
|
|
|
|
time.Now(), &p1.keyStore.keySet.Keys[0])
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
failZone, err := generateGCPToken(p1.ServiceAccounts[0],
|
|
|
|
"https://accounts.google.com", p1.GetID(),
|
|
|
|
"instance-id", "instance-name", "project-id", "",
|
|
|
|
time.Now(), &p1.keyStore.keySet.Keys[0])
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
|
|
|
|
type args struct {
|
|
|
|
token string
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
gcp *GCP
|
|
|
|
args args
|
2019-04-24 20:05:46 +00:00
|
|
|
wantLen int
|
2019-12-20 21:30:05 +00:00
|
|
|
code int
|
2019-04-18 23:01:30 +00:00
|
|
|
wantErr bool
|
|
|
|
}{
|
2019-12-20 21:30:05 +00:00
|
|
|
{"ok", p1, args{t1}, 4, http.StatusOK, false},
|
|
|
|
{"ok", p2, args{t2}, 6, http.StatusOK, false},
|
|
|
|
{"ok", p3, args{t3}, 4, http.StatusOK, false},
|
|
|
|
{"fail token", p1, args{"token"}, 0, http.StatusUnauthorized, true},
|
|
|
|
{"fail key", p1, args{failKey}, 0, http.StatusUnauthorized, true},
|
|
|
|
{"fail iss", p1, args{failIss}, 0, http.StatusUnauthorized, true},
|
|
|
|
{"fail aud", p1, args{failAud}, 0, http.StatusUnauthorized, true},
|
|
|
|
{"fail exp", p1, args{failExp}, 0, http.StatusUnauthorized, true},
|
|
|
|
{"fail nbf", p1, args{failNbf}, 0, http.StatusUnauthorized, true},
|
|
|
|
{"fail service account", p1, args{failServiceAccount}, 0, http.StatusUnauthorized, true},
|
|
|
|
{"fail invalid project id", p3, args{failInvalidProjectID}, 0, http.StatusUnauthorized, true},
|
|
|
|
{"fail invalid instance age", p3, args{failInvalidInstanceAge}, 0, http.StatusUnauthorized, true},
|
|
|
|
{"fail instance id", p1, args{failInstanceID}, 0, http.StatusUnauthorized, true},
|
|
|
|
{"fail instance name", p1, args{failInstanceName}, 0, http.StatusUnauthorized, true},
|
|
|
|
{"fail project id", p1, args{failProjectID}, 0, http.StatusUnauthorized, true},
|
|
|
|
{"fail zone", p1, args{failZone}, 0, http.StatusUnauthorized, true},
|
2019-04-18 23:01:30 +00:00
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2019-07-30 01:24:34 +00:00
|
|
|
ctx := NewContextWithMethod(context.Background(), SignMethod)
|
|
|
|
got, err := tt.gcp.AuthorizeSign(ctx, tt.args.token)
|
2019-04-18 23:01:30 +00:00
|
|
|
if (err != nil) != tt.wantErr {
|
|
|
|
t.Errorf("GCP.AuthorizeSign() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
|
return
|
2019-12-20 21:30:05 +00:00
|
|
|
} else if err != nil {
|
|
|
|
sc, ok := err.(errs.StatusCoder)
|
|
|
|
assert.Fatal(t, ok, "error does not implement StatusCoder interface")
|
|
|
|
assert.Equals(t, sc.StatusCode(), tt.code)
|
|
|
|
} else {
|
|
|
|
assert.Len(t, tt.wantLen, got)
|
2019-04-18 23:01:30 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-14 23:26:37 +00:00
|
|
|
func TestGCP_AuthorizeSSHSign(t *testing.T) {
|
2019-08-01 01:22:21 +00:00
|
|
|
tm, fn := mockNow()
|
|
|
|
defer fn()
|
|
|
|
|
|
|
|
p1, err := generateGCP()
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
|
2019-12-20 21:30:05 +00:00
|
|
|
p2, err := generateGCP()
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
// disable sshCA
|
|
|
|
disable := false
|
|
|
|
p2.Claims = &Claims{EnableSSHCA: &disable}
|
|
|
|
p2.claimer, err = NewClaimer(p2.Claims, globalProvisionerClaims)
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
|
2019-08-01 01:22:21 +00:00
|
|
|
t1, err := generateGCPToken(p1.ServiceAccounts[0],
|
|
|
|
"https://accounts.google.com", p1.GetID(),
|
|
|
|
"instance-id", "instance-name", "project-id", "zone",
|
|
|
|
time.Now(), &p1.keyStore.keySet.Keys[0])
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
|
|
|
|
key, err := generateJSONWebKey()
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
|
|
|
|
signer, err := generateJSONWebKey()
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
|
2019-09-11 00:04:13 +00:00
|
|
|
pub := key.Public().Key
|
|
|
|
rsa2048, err := rsa.GenerateKey(rand.Reader, 2048)
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
rsa1024, err := rsa.GenerateKey(rand.Reader, 1024)
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
|
2019-08-01 01:22:21 +00:00
|
|
|
hostDuration := p1.claimer.DefaultHostSSHCertDuration()
|
|
|
|
expectedHostOptions := &SSHOptions{
|
|
|
|
CertType: "host", Principals: []string{"instance-name.c.project-id.internal", "instance-name.zone.c.project-id.internal"},
|
|
|
|
ValidAfter: NewTimeDuration(tm), ValidBefore: NewTimeDuration(tm.Add(hostDuration)),
|
|
|
|
}
|
|
|
|
expectedHostOptionsPrincipal1 := &SSHOptions{
|
|
|
|
CertType: "host", Principals: []string{"instance-name.c.project-id.internal"},
|
|
|
|
ValidAfter: NewTimeDuration(tm), ValidBefore: NewTimeDuration(tm.Add(hostDuration)),
|
|
|
|
}
|
|
|
|
expectedHostOptionsPrincipal2 := &SSHOptions{
|
|
|
|
CertType: "host", Principals: []string{"instance-name.zone.c.project-id.internal"},
|
|
|
|
ValidAfter: NewTimeDuration(tm), ValidBefore: NewTimeDuration(tm.Add(hostDuration)),
|
|
|
|
}
|
|
|
|
|
|
|
|
type args struct {
|
|
|
|
token string
|
|
|
|
sshOpts SSHOptions
|
2019-09-11 00:04:13 +00:00
|
|
|
key interface{}
|
2019-08-01 01:22:21 +00:00
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
gcp *GCP
|
|
|
|
args args
|
|
|
|
expected *SSHOptions
|
2019-12-20 21:30:05 +00:00
|
|
|
code int
|
2019-08-01 01:22:21 +00:00
|
|
|
wantErr bool
|
|
|
|
wantSignErr bool
|
|
|
|
}{
|
2019-12-20 21:30:05 +00:00
|
|
|
{"ok", p1, args{t1, SSHOptions{}, pub}, expectedHostOptions, http.StatusOK, false, false},
|
|
|
|
{"ok-rsa2048", p1, args{t1, SSHOptions{}, rsa2048.Public()}, expectedHostOptions, http.StatusOK, false, false},
|
|
|
|
{"ok-type", p1, args{t1, SSHOptions{CertType: "host"}, pub}, expectedHostOptions, http.StatusOK, false, false},
|
|
|
|
{"ok-principals", p1, args{t1, SSHOptions{Principals: []string{"instance-name.c.project-id.internal", "instance-name.zone.c.project-id.internal"}}, pub}, expectedHostOptions, http.StatusOK, false, false},
|
|
|
|
{"ok-principal1", p1, args{t1, SSHOptions{Principals: []string{"instance-name.c.project-id.internal"}}, pub}, expectedHostOptionsPrincipal1, http.StatusOK, false, false},
|
|
|
|
{"ok-principal2", p1, args{t1, SSHOptions{Principals: []string{"instance-name.zone.c.project-id.internal"}}, pub}, expectedHostOptionsPrincipal2, http.StatusOK, false, false},
|
|
|
|
{"ok-options", p1, args{t1, SSHOptions{CertType: "host", Principals: []string{"instance-name.c.project-id.internal", "instance-name.zone.c.project-id.internal"}}, pub}, expectedHostOptions, http.StatusOK, false, false},
|
|
|
|
{"fail-rsa1024", p1, args{t1, SSHOptions{}, rsa1024.Public()}, expectedHostOptions, http.StatusOK, false, true},
|
|
|
|
{"fail-type", p1, args{t1, SSHOptions{CertType: "user"}, pub}, nil, http.StatusOK, false, true},
|
|
|
|
{"fail-principal", p1, args{t1, SSHOptions{Principals: []string{"smallstep.com"}}, pub}, nil, http.StatusOK, false, true},
|
|
|
|
{"fail-extra-principal", p1, args{t1, SSHOptions{Principals: []string{"instance-name.c.project-id.internal", "instance-name.zone.c.project-id.internal", "smallstep.com"}}, pub}, nil, http.StatusOK, false, true},
|
|
|
|
{"fail-sshCA-disabled", p2, args{"foo", SSHOptions{}, pub}, expectedHostOptions, http.StatusUnauthorized, true, false},
|
|
|
|
{"fail-invalid-token", p1, args{"foo", SSHOptions{}, pub}, expectedHostOptions, http.StatusUnauthorized, true, false},
|
2019-08-01 01:22:21 +00:00
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2019-12-20 21:30:05 +00:00
|
|
|
got, err := tt.gcp.AuthorizeSSHSign(context.Background(), tt.args.token)
|
2019-08-01 01:22:21 +00:00
|
|
|
if (err != nil) != tt.wantErr {
|
2019-11-14 23:26:37 +00:00
|
|
|
t.Errorf("GCP.AuthorizeSSHSign() error = %v, wantErr %v", err, tt.wantErr)
|
2019-08-01 01:22:21 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if err != nil {
|
2019-12-20 21:30:05 +00:00
|
|
|
sc, ok := err.(errs.StatusCoder)
|
|
|
|
assert.Fatal(t, ok, "error does not implement StatusCoder interface")
|
|
|
|
assert.Equals(t, sc.StatusCode(), tt.code)
|
2019-08-01 01:22:21 +00:00
|
|
|
assert.Nil(t, got)
|
|
|
|
} else if assert.NotNil(t, got) {
|
2019-09-11 00:04:13 +00:00
|
|
|
cert, err := signSSHCertificate(tt.args.key, tt.args.sshOpts, got, signer.Key.(crypto.Signer))
|
2019-08-01 01:22:21 +00:00
|
|
|
if (err != nil) != tt.wantSignErr {
|
|
|
|
t.Errorf("SignSSH error = %v, wantSignErr %v", err, tt.wantSignErr)
|
|
|
|
} else {
|
|
|
|
if tt.wantSignErr {
|
|
|
|
assert.Nil(t, cert)
|
|
|
|
} else {
|
|
|
|
assert.NoError(t, validateSSHCertificate(cert, tt.expected))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-20 21:30:05 +00:00
|
|
|
func TestGCP_AuthorizeRenew(t *testing.T) {
|
2019-04-18 23:01:30 +00:00
|
|
|
p1, err := generateGCP()
|
|
|
|
assert.FatalError(t, err)
|
|
|
|
p2, err := generateGCP()
|
|
|
|
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
|
|
|
|
prov *GCP
|
|
|
|
args args
|
2019-12-20 21:30:05 +00:00
|
|
|
code int
|
2019-04-18 23:01:30 +00:00
|
|
|
wantErr bool
|
|
|
|
}{
|
2019-12-20 21:30:05 +00:00
|
|
|
{"ok", p1, args{nil}, http.StatusOK, false},
|
|
|
|
{"fail/renewal-disabled", p2, args{nil}, http.StatusUnauthorized, true},
|
2019-04-18 23:01:30 +00:00
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2020-01-24 06:04:34 +00:00
|
|
|
if err := tt.prov.AuthorizeRenew(context.Background(), tt.args.cert); (err != nil) != tt.wantErr {
|
2019-12-20 21:30:05 +00:00
|
|
|
t.Errorf("GCP.AuthorizeRenew() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
|
} else if err != nil {
|
|
|
|
sc, ok := err.(errs.StatusCoder)
|
|
|
|
assert.Fatal(t, ok, "error does not implement StatusCoder interface")
|
|
|
|
assert.Equals(t, sc.StatusCode(), tt.code)
|
2019-04-18 23:01:30 +00:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|