2019-03-08 03:30:17 +00:00
|
|
|
package provisioner
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto"
|
2019-04-25 02:52:58 +00:00
|
|
|
"crypto/rand"
|
|
|
|
"crypto/sha256"
|
|
|
|
"crypto/x509"
|
|
|
|
"encoding/base64"
|
2019-03-08 03:30:17 +00:00
|
|
|
"encoding/hex"
|
|
|
|
"encoding/json"
|
2019-04-25 02:52:58 +00:00
|
|
|
"encoding/pem"
|
2019-05-08 02:07:49 +00:00
|
|
|
"fmt"
|
2019-03-11 17:40:55 +00:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
2021-11-12 23:46:34 +00:00
|
|
|
"os"
|
2020-12-17 21:35:14 +00:00
|
|
|
"strings"
|
2019-03-08 03:30:17 +00:00
|
|
|
"time"
|
|
|
|
|
2019-04-25 02:52:58 +00:00
|
|
|
"github.com/pkg/errors"
|
2020-08-24 21:44:11 +00:00
|
|
|
"go.step.sm/crypto/jose"
|
2020-08-14 22:33:50 +00:00
|
|
|
"go.step.sm/crypto/pemutil"
|
2020-08-10 18:26:51 +00:00
|
|
|
"go.step.sm/crypto/randutil"
|
2019-12-20 21:30:05 +00:00
|
|
|
"golang.org/x/crypto/ssh"
|
2019-03-08 03:30:17 +00:00
|
|
|
)
|
|
|
|
|
2019-09-05 01:31:09 +00:00
|
|
|
var (
|
2022-04-13 22:11:54 +00:00
|
|
|
defaultDisableRenewal = false
|
|
|
|
defaultAllowRenewalAfterExpiry = false
|
|
|
|
defaultEnableSSHCA = true
|
|
|
|
globalProvisionerClaims = Claims{
|
|
|
|
MinTLSDur: &Duration{5 * time.Minute},
|
|
|
|
MaxTLSDur: &Duration{24 * time.Hour},
|
|
|
|
DefaultTLSDur: &Duration{24 * time.Hour},
|
|
|
|
MinUserSSHDur: &Duration{Duration: 5 * time.Minute}, // User SSH certs
|
|
|
|
MaxUserSSHDur: &Duration{Duration: 24 * time.Hour},
|
|
|
|
DefaultUserSSHDur: &Duration{Duration: 16 * time.Hour},
|
|
|
|
MinHostSSHDur: &Duration{Duration: 5 * time.Minute}, // Host SSH certs
|
|
|
|
MaxHostSSHDur: &Duration{Duration: 30 * 24 * time.Hour},
|
|
|
|
DefaultHostSSHDur: &Duration{Duration: 30 * 24 * time.Hour},
|
|
|
|
EnableSSHCA: &defaultEnableSSHCA,
|
|
|
|
DisableRenewal: &defaultDisableRenewal,
|
|
|
|
AllowRenewalAfterExpiry: &defaultAllowRenewalAfterExpiry,
|
2019-09-05 01:31:09 +00:00
|
|
|
}
|
|
|
|
testAudiences = Audiences{
|
2019-11-14 23:26:37 +00:00
|
|
|
Sign: []string{"https://ca.smallstep.com/1.0/sign", "https://ca.smallstep.com/sign"},
|
|
|
|
Revoke: []string{"https://ca.smallstep.com/1.0/revoke", "https://ca.smallstep.com/revoke"},
|
|
|
|
SSHSign: []string{"https://ca.smallstep.com/1.0/ssh/sign"},
|
|
|
|
SSHRevoke: []string{"https://ca.smallstep.com/1.0/ssh/revoke"},
|
|
|
|
SSHRenew: []string{"https://ca.smallstep.com/1.0/ssh/renew"},
|
|
|
|
SSHRekey: []string{"https://ca.smallstep.com/1.0/ssh/rekey"},
|
2019-09-05 01:31:09 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2019-04-25 02:52:58 +00:00
|
|
|
const awsTestCertificate = `-----BEGIN CERTIFICATE-----
|
|
|
|
MIICFTCCAX6gAwIBAgIRAKmbVVYAl/1XEqRfF3eJ97MwDQYJKoZIhvcNAQELBQAw
|
|
|
|
GDEWMBQGA1UEAxMNQVdTIFRlc3QgQ2VydDAeFw0xOTA0MjQyMjU3MzlaFw0yOTA0
|
|
|
|
MjEyMjU3MzlaMBgxFjAUBgNVBAMTDUFXUyBUZXN0IENlcnQwgZ8wDQYJKoZIhvcN
|
|
|
|
AQEBBQADgY0AMIGJAoGBAOHMmMXwbXN90SoRl/xXAcJs5TacaVYJ5iNAVWM5KYyF
|
|
|
|
+JwqYuJp/umLztFUi0oX0luu3EzD4KurVeUJSzZjTFTX1d/NX6hA45+bvdSUOcgV
|
|
|
|
UghO+2uhBZ4SNFxFRZ7SKvoWIN195l5bVX6/60Eo6+kUCKCkyxW4V/ksWzdXjHnf
|
|
|
|
AgMBAAGjXzBdMA4GA1UdDwEB/wQEAwIBBjASBgNVHRMBAf8ECDAGAQH/AgEBMB0G
|
|
|
|
A1UdDgQWBBRHfLOjEddK/CWCIHNg8Oc/oJa1IzAYBgNVHREEETAPgg1BV1MgVGVz
|
|
|
|
dCBDZXJ0MA0GCSqGSIb3DQEBCwUAA4GBAKNCiVM9eGb9dW2xNyHaHAmmy7ERB2OJ
|
|
|
|
7oXHfLjooOavk9lU/Gs2jfX/JSBa84+DzWg9ShmCNLti8CxU/dhzXW7jE/5CcdTa
|
|
|
|
DCA6B3Yl5TmfG9+D9dtFqRB2CiMgNcsJJE5Dc6pDwBIiSj/MkE0AaGVQmSwn6Cb6
|
|
|
|
vX1TAxqeWJHq
|
|
|
|
-----END CERTIFICATE-----`
|
|
|
|
|
|
|
|
const awsTestKey = `-----BEGIN RSA PRIVATE KEY-----
|
|
|
|
MIICXAIBAAKBgQDhzJjF8G1zfdEqEZf8VwHCbOU2nGlWCeYjQFVjOSmMhficKmLi
|
|
|
|
af7pi87RVItKF9JbrtxMw+Crq1XlCUs2Y0xU19XfzV+oQOOfm73UlDnIFVIITvtr
|
|
|
|
oQWeEjRcRUWe0ir6FiDdfeZeW1V+v+tBKOvpFAigpMsVuFf5LFs3V4x53wIDAQAB
|
|
|
|
AoGADZQFF9oWatyFCHeYYSdGRs/PlNIhD3h262XB/L6CPh4MTi/KVH01RAwROstP
|
|
|
|
uPvnvXWtb7xTtV8PQj+l0zZzb4W/DLCSBdoRwpuNXyffUCtbI22jPupTsVu+ENWR
|
|
|
|
3x7HHzoZYjU45ADSTMxEtwD7/zyNgpRKjIA2HYpkt+fI27ECQQD5/AOr9/yQD73x
|
|
|
|
cquF+FWahWgDL25YeMwdfe1HfpUxUxd9kJJKieB8E2BtBAv9XNguxIBpf7VlAKsF
|
|
|
|
NFhdfWFHAkEA5zuX8vqDecSzyNNEQd3tugxt1pGOXNesHzuPbdlw3ppN9Rbd93an
|
|
|
|
uU2TaAvTjr/3EkxulYNRmHs+RSVK54+uqQJAKWurhBQMAibJlzcj2ofiTz8pk9WJ
|
|
|
|
GBmz4HMcHMuJlumoq8KHqtgbnRNs18Ni5TE8FMu0Z0ak3L52l98rgRokQwJBAJS8
|
|
|
|
9KTLF79AFBVeME3eH4jJbe3TeyulX4ZHnZ8fe0b1IqhAqU8A+CpuCB+pW9A7Ewam
|
|
|
|
O4vZCKd4vzljH6eL+OECQHHxhYoTW7lFpKGnUDG9fPZ3eYzWpgka6w1vvBk10BAu
|
|
|
|
6fbwppM9pQ7DPMg7V6YGEjjT0gX9B9TttfHxGhvtZNQ=
|
|
|
|
-----END RSA PRIVATE KEY-----`
|
|
|
|
|
2019-03-08 23:08:18 +00:00
|
|
|
func must(args ...interface{}) []interface{} {
|
|
|
|
if l := len(args); l > 0 && args[l-1] != nil {
|
|
|
|
if err, ok := args[l-1].(error); ok {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return args
|
|
|
|
}
|
|
|
|
|
2019-03-08 03:30:17 +00:00
|
|
|
func generateJSONWebKey() (*jose.JSONWebKey, error) {
|
|
|
|
jwk, err := jose.GenerateJWK("EC", "P-256", "ES256", "sig", "", 0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
fp, err := jwk.Thumbprint(crypto.SHA256)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
jwk.KeyID = string(hex.EncodeToString(fp))
|
|
|
|
return jwk, nil
|
|
|
|
}
|
|
|
|
|
2019-03-08 23:08:18 +00:00
|
|
|
func generateJSONWebKeySet(n int) (jose.JSONWebKeySet, error) {
|
|
|
|
var keySet jose.JSONWebKeySet
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
key, err := generateJSONWebKey()
|
|
|
|
if err != nil {
|
|
|
|
return jose.JSONWebKeySet{}, err
|
|
|
|
}
|
2019-03-11 19:48:46 +00:00
|
|
|
keySet.Keys = append(keySet.Keys, *key)
|
2019-03-08 23:08:18 +00:00
|
|
|
}
|
|
|
|
return keySet, nil
|
|
|
|
}
|
|
|
|
|
2019-03-08 03:30:17 +00:00
|
|
|
func encryptJSONWebKey(jwk *jose.JSONWebKey) (*jose.JSONWebEncryption, error) {
|
|
|
|
b, err := json.Marshal(jwk)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
salt, err := randutil.Salt(jose.PBKDF2SaltSize)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
opts := new(jose.EncrypterOptions)
|
|
|
|
opts.WithContentType(jose.ContentType("jwk+json"))
|
|
|
|
recipient := jose.Recipient{
|
|
|
|
Algorithm: jose.PBES2_HS256_A128KW,
|
|
|
|
Key: []byte("password"),
|
|
|
|
PBES2Count: jose.PBKDF2Iterations,
|
|
|
|
PBES2Salt: salt,
|
|
|
|
}
|
|
|
|
encrypter, err := jose.NewEncrypter(jose.DefaultEncAlgorithm, recipient, opts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return encrypter.Encrypt(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
func decryptJSONWebKey(key string) (*jose.JSONWebKey, error) {
|
|
|
|
enc, err := jose.ParseEncrypted(key)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
b, err := enc.Decrypt([]byte("password"))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
jwk := new(jose.JSONWebKey)
|
|
|
|
if err := json.Unmarshal(b, jwk); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return jwk, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateJWK() (*JWK, error) {
|
|
|
|
name, err := randutil.Alphanumeric(10)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
jwk, err := generateJSONWebKey()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
jwe, err := encryptJSONWebKey(jwk)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
public := jwk.Public()
|
|
|
|
encrypted, err := jwe.CompactSerialize()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-03-10 02:43:45 +00:00
|
|
|
|
|
|
|
p := &JWK{
|
2019-03-08 03:30:17 +00:00
|
|
|
Name: name,
|
|
|
|
Type: "JWK",
|
|
|
|
Key: &public,
|
|
|
|
EncryptedKey: encrypted,
|
2019-03-09 01:24:58 +00:00
|
|
|
Claims: &globalProvisionerClaims,
|
2022-03-10 02:43:45 +00:00
|
|
|
}
|
|
|
|
p.ctl, err = NewController(p, p.Claims, Config{
|
|
|
|
Audiences: testAudiences,
|
|
|
|
})
|
|
|
|
return p, err
|
2019-03-08 03:30:17 +00:00
|
|
|
}
|
|
|
|
|
2019-10-18 20:52:28 +00:00
|
|
|
func generateK8sSA(inputPubKey interface{}) (*K8sSA, error) {
|
2021-11-12 23:46:34 +00:00
|
|
|
fooPubB, err := os.ReadFile("./testdata/certs/foo.pub")
|
2019-10-18 20:52:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
fooPub, err := pemutil.ParseKey(fooPubB)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-11-12 23:46:34 +00:00
|
|
|
barPubB, err := os.ReadFile("./testdata/certs/bar.pub")
|
2019-10-18 20:52:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
barPub, err := pemutil.ParseKey(barPubB)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
pubKeys := []interface{}{fooPub, barPub}
|
|
|
|
if inputPubKey != nil {
|
|
|
|
pubKeys = append(pubKeys, inputPubKey)
|
|
|
|
}
|
|
|
|
|
2022-03-10 02:43:45 +00:00
|
|
|
p := &K8sSA{
|
|
|
|
Name: K8sSAName,
|
|
|
|
Type: "K8sSA",
|
|
|
|
Claims: &globalProvisionerClaims,
|
|
|
|
pubKeys: pubKeys,
|
|
|
|
}
|
|
|
|
p.ctl, err = NewController(p, p.Claims, Config{
|
|
|
|
Audiences: testAudiences,
|
|
|
|
})
|
|
|
|
return p, err
|
2019-10-18 20:52:28 +00:00
|
|
|
}
|
|
|
|
|
2019-12-20 21:30:05 +00:00
|
|
|
func generateSSHPOP() (*SSHPOP, error) {
|
|
|
|
name, err := randutil.Alphanumeric(10)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-11-12 23:46:34 +00:00
|
|
|
userB, err := os.ReadFile("./testdata/certs/ssh_user_ca_key.pub")
|
2019-12-20 21:30:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
userKey, _, _, _, err := ssh.ParseAuthorizedKey(userB)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-11-12 23:46:34 +00:00
|
|
|
hostB, err := os.ReadFile("./testdata/certs/ssh_host_ca_key.pub")
|
2019-12-20 21:30:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
hostKey, _, _, _, err := ssh.ParseAuthorizedKey(hostB)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-03-10 02:43:45 +00:00
|
|
|
p := &SSHPOP{
|
|
|
|
Name: name,
|
|
|
|
Type: "SSHPOP",
|
|
|
|
Claims: &globalProvisionerClaims,
|
2019-12-20 21:30:05 +00:00
|
|
|
sshPubKeys: &SSHKeys{
|
|
|
|
UserKeys: []ssh.PublicKey{userKey},
|
|
|
|
HostKeys: []ssh.PublicKey{hostKey},
|
|
|
|
},
|
2022-03-10 02:43:45 +00:00
|
|
|
}
|
|
|
|
p.ctl, err = NewController(p, p.Claims, Config{
|
|
|
|
Audiences: testAudiences,
|
|
|
|
})
|
|
|
|
return p, err
|
2019-12-20 21:30:05 +00:00
|
|
|
}
|
|
|
|
|
2019-09-05 01:31:09 +00:00
|
|
|
func generateX5C(root []byte) (*X5C, error) {
|
|
|
|
if root == nil {
|
|
|
|
root = []byte(`-----BEGIN CERTIFICATE-----
|
|
|
|
MIIBhTCCASqgAwIBAgIRAMalM7pKi0GCdKjO6u88OyowCgYIKoZIzj0EAwIwFDES
|
|
|
|
MBAGA1UEAxMJcm9vdC10ZXN0MCAXDTE5MTAwMjAyMzk0OFoYDzIxMTkwOTA4MDIz
|
|
|
|
OTQ4WjAUMRIwEAYDVQQDEwlyb290LXRlc3QwWTATBgcqhkjOPQIBBggqhkjOPQMB
|
|
|
|
BwNCAAS29QTCXUu7cx9sa9wZPpRSFq/zXaw8Ai3EIygayrBsKnX42U2atBUjcBZO
|
|
|
|
BWL6A+PpLzU9ja867U5SYNHERS+Oo1swWTAOBgNVHQ8BAf8EBAMCAQYwEgYDVR0T
|
|
|
|
AQH/BAgwBgEB/wIBATAdBgNVHQ4EFgQUpHS7FfaQ5bCrTxUeu6R2ZC3VGOowFAYD
|
|
|
|
VR0RBA0wC4IJcm9vdC10ZXN0MAoGCCqGSM49BAMCA0kAMEYCIQC2vgqwla0u8LHH
|
|
|
|
1MHob14qvS5o76HautbIBW7fcHzz5gIhAIx5A2+wkJYX4026kqaZCk/1sAwTxSGY
|
|
|
|
M46l92gdOozT
|
|
|
|
-----END CERTIFICATE-----`)
|
|
|
|
}
|
|
|
|
|
|
|
|
name, err := randutil.Alphanumeric(10)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
rootPool := x509.NewCertPool()
|
|
|
|
|
|
|
|
var (
|
|
|
|
block *pem.Block
|
|
|
|
rest = root
|
|
|
|
)
|
|
|
|
for rest != nil {
|
|
|
|
block, rest = pem.Decode(rest)
|
|
|
|
if block == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
cert, err := x509.ParseCertificate(block.Bytes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "error parsing x509 certificate from PEM block")
|
|
|
|
}
|
|
|
|
rootPool.AddCert(cert)
|
|
|
|
}
|
2022-03-10 02:43:45 +00:00
|
|
|
p := &X5C{
|
|
|
|
Name: name,
|
|
|
|
Type: "X5C",
|
|
|
|
Roots: root,
|
|
|
|
Claims: &globalProvisionerClaims,
|
|
|
|
rootPool: rootPool,
|
|
|
|
}
|
|
|
|
p.ctl, err = NewController(p, p.Claims, Config{
|
|
|
|
Audiences: testAudiences,
|
|
|
|
})
|
|
|
|
return p, err
|
2019-09-05 01:31:09 +00:00
|
|
|
}
|
|
|
|
|
2019-03-08 03:30:17 +00:00
|
|
|
func generateOIDC() (*OIDC, error) {
|
|
|
|
name, err := randutil.Alphanumeric(10)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
clientID, err := randutil.Alphanumeric(10)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
issuer, err := randutil.Alphanumeric(10)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
jwk, err := generateJSONWebKey()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-03-10 02:43:45 +00:00
|
|
|
p := &OIDC{
|
2019-03-08 03:30:17 +00:00
|
|
|
Name: name,
|
|
|
|
Type: "OIDC",
|
|
|
|
ClientID: clientID,
|
|
|
|
ConfigurationEndpoint: "https://example.com/.well-known/openid-configuration",
|
2019-03-09 01:24:58 +00:00
|
|
|
Claims: &globalProvisionerClaims,
|
2019-03-08 03:30:17 +00:00
|
|
|
configuration: openIDConfiguration{
|
|
|
|
Issuer: issuer,
|
|
|
|
JWKSetURI: "https://example.com/.well-known/jwks",
|
|
|
|
},
|
|
|
|
keyStore: &keyStore{
|
2019-03-08 20:19:44 +00:00
|
|
|
keySet: jose.JSONWebKeySet{Keys: []jose.JSONWebKey{*jwk}},
|
2019-03-08 03:30:17 +00:00
|
|
|
expiry: time.Now().Add(24 * time.Hour),
|
|
|
|
},
|
2022-03-10 02:43:45 +00:00
|
|
|
}
|
|
|
|
p.ctl, err = NewController(p, p.Claims, Config{
|
|
|
|
Audiences: testAudiences,
|
|
|
|
})
|
|
|
|
return p, err
|
2019-03-08 03:30:17 +00:00
|
|
|
}
|
|
|
|
|
2019-04-18 23:01:30 +00:00
|
|
|
func generateGCP() (*GCP, error) {
|
|
|
|
name, err := randutil.Alphanumeric(10)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
serviceAccount, err := randutil.Alphanumeric(10)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
jwk, err := generateJSONWebKey()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-03-10 02:43:45 +00:00
|
|
|
p := &GCP{
|
2019-04-18 23:01:30 +00:00
|
|
|
Type: "GCP",
|
|
|
|
Name: name,
|
|
|
|
ServiceAccounts: []string{serviceAccount},
|
|
|
|
Claims: &globalProvisionerClaims,
|
2019-04-24 18:30:37 +00:00
|
|
|
config: newGCPConfig(),
|
2019-04-18 23:01:30 +00:00
|
|
|
keyStore: &keyStore{
|
|
|
|
keySet: jose.JSONWebKeySet{Keys: []jose.JSONWebKey{*jwk}},
|
|
|
|
expiry: time.Now().Add(24 * time.Hour),
|
|
|
|
},
|
2022-03-10 02:43:45 +00:00
|
|
|
}
|
|
|
|
p.ctl, err = NewController(p, p.Claims, Config{
|
|
|
|
Audiences: testAudiences.WithFragment("gcp/" + name),
|
|
|
|
})
|
|
|
|
return p, err
|
2019-04-18 23:01:30 +00:00
|
|
|
}
|
|
|
|
|
2019-04-25 02:52:58 +00:00
|
|
|
func generateAWS() (*AWS, error) {
|
|
|
|
name, err := randutil.Alphanumeric(10)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
accountID, err := randutil.Alphanumeric(10)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
block, _ := pem.Decode([]byte(awsTestCertificate))
|
|
|
|
if block == nil || block.Type != "CERTIFICATE" {
|
|
|
|
return nil, errors.New("error decoding AWS certificate")
|
|
|
|
}
|
|
|
|
cert, err := x509.ParseCertificate(block.Bytes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "error parsing AWS certificate")
|
|
|
|
}
|
2022-03-10 02:43:45 +00:00
|
|
|
p := &AWS{
|
2020-05-20 17:15:51 +00:00
|
|
|
Type: "AWS",
|
|
|
|
Name: name,
|
|
|
|
Accounts: []string{accountID},
|
|
|
|
Claims: &globalProvisionerClaims,
|
|
|
|
IMDSVersions: []string{"v2", "v1"},
|
2019-04-25 02:52:58 +00:00
|
|
|
config: &awsConfig{
|
|
|
|
identityURL: awsIdentityURL,
|
|
|
|
signatureURL: awsSignatureURL,
|
2020-05-20 13:39:19 +00:00
|
|
|
tokenURL: awsAPITokenURL,
|
|
|
|
tokenTTL: awsAPITokenTTL,
|
2020-10-14 00:51:24 +00:00
|
|
|
certificates: []*x509.Certificate{cert},
|
2019-04-25 02:52:58 +00:00
|
|
|
signatureAlgorithm: awsSignatureAlgorithm,
|
|
|
|
},
|
2022-03-10 02:43:45 +00:00
|
|
|
}
|
|
|
|
p.ctl, err = NewController(p, p.Claims, Config{
|
|
|
|
Audiences: testAudiences.WithFragment("aws/" + name),
|
|
|
|
})
|
|
|
|
return p, err
|
2019-04-25 02:52:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func generateAWSWithServer() (*AWS, *httptest.Server, error) {
|
|
|
|
aws, err := generateAWS()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
block, _ := pem.Decode([]byte(awsTestKey))
|
|
|
|
if block == nil || block.Type != "RSA PRIVATE KEY" {
|
|
|
|
return nil, nil, errors.New("error decoding AWS key")
|
|
|
|
}
|
|
|
|
key, err := x509.ParsePKCS1PrivateKey(block.Bytes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, errors.Wrap(err, "error parsing AWS private key")
|
|
|
|
}
|
|
|
|
doc, err := json.MarshalIndent(awsInstanceIdentityDocument{
|
|
|
|
AccountID: aws.Accounts[0],
|
|
|
|
Architecture: "x86_64",
|
|
|
|
AvailabilityZone: "us-west-2b",
|
2019-07-15 22:52:36 +00:00
|
|
|
ImageID: "image-id",
|
|
|
|
InstanceID: "instance-id",
|
2019-04-25 02:52:58 +00:00
|
|
|
InstanceType: "t2.micro",
|
|
|
|
PendingTime: time.Now(),
|
|
|
|
PrivateIP: "127.0.0.1",
|
|
|
|
Region: "us-west-1",
|
|
|
|
Version: "2017-09-30",
|
|
|
|
}, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
sum := sha256.Sum256(doc)
|
|
|
|
signature, err := key.Sign(rand.Reader, sum[:], crypto.SHA256)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, errors.Wrap(err, "error signing document")
|
|
|
|
}
|
2020-05-20 13:03:35 +00:00
|
|
|
token := "AQAEAEEO9-7Z88ewKFpboZuDlFYWz9A3AN-wMOVzjEhfAyXW31BvVw=="
|
2019-04-25 02:52:58 +00:00
|
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
switch r.URL.Path {
|
|
|
|
case "/latest/dynamic/instance-identity/document":
|
2020-05-20 13:03:35 +00:00
|
|
|
// check for API token
|
|
|
|
if r.Header.Get("X-aws-ec2-metadata-token") != token {
|
|
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
|
|
w.Write([]byte("401 Unauthorized"))
|
|
|
|
}
|
2019-04-25 02:52:58 +00:00
|
|
|
w.Write(doc)
|
|
|
|
case "/latest/dynamic/instance-identity/signature":
|
2020-05-20 13:03:35 +00:00
|
|
|
// check for API token
|
|
|
|
if r.Header.Get("X-aws-ec2-metadata-token") != token {
|
|
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
|
|
w.Write([]byte("401 Unauthorized"))
|
|
|
|
}
|
2019-04-25 02:52:58 +00:00
|
|
|
w.Write([]byte(base64.StdEncoding.EncodeToString(signature)))
|
2020-05-20 13:03:35 +00:00
|
|
|
case "/latest/api/token":
|
|
|
|
w.Write([]byte(token))
|
2019-04-25 02:52:58 +00:00
|
|
|
case "/bad-document":
|
|
|
|
w.Write([]byte("{}"))
|
|
|
|
case "/bad-signature":
|
|
|
|
w.Write([]byte("YmFkLXNpZ25hdHVyZQo="))
|
2019-07-15 22:52:36 +00:00
|
|
|
case "/bad-json":
|
|
|
|
w.Write([]byte("{"))
|
2019-04-25 02:52:58 +00:00
|
|
|
default:
|
|
|
|
http.NotFound(w, r)
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
aws.config.identityURL = srv.URL + "/latest/dynamic/instance-identity/document"
|
|
|
|
aws.config.signatureURL = srv.URL + "/latest/dynamic/instance-identity/signature"
|
2020-05-20 13:03:35 +00:00
|
|
|
aws.config.tokenURL = srv.URL + "/latest/api/token"
|
2019-04-25 02:52:58 +00:00
|
|
|
return aws, srv, nil
|
|
|
|
}
|
|
|
|
|
2020-07-22 23:52:06 +00:00
|
|
|
func generateAWSV1Only() (*AWS, error) {
|
|
|
|
name, err := randutil.Alphanumeric(10)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
accountID, err := randutil.Alphanumeric(10)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
block, _ := pem.Decode([]byte(awsTestCertificate))
|
|
|
|
if block == nil || block.Type != "CERTIFICATE" {
|
|
|
|
return nil, errors.New("error decoding AWS certificate")
|
|
|
|
}
|
|
|
|
cert, err := x509.ParseCertificate(block.Bytes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "error parsing AWS certificate")
|
|
|
|
}
|
2022-03-10 02:43:45 +00:00
|
|
|
p := &AWS{
|
2020-07-22 23:52:06 +00:00
|
|
|
Type: "AWS",
|
|
|
|
Name: name,
|
|
|
|
Accounts: []string{accountID},
|
|
|
|
Claims: &globalProvisionerClaims,
|
|
|
|
IMDSVersions: []string{"v1"},
|
|
|
|
config: &awsConfig{
|
|
|
|
identityURL: awsIdentityURL,
|
|
|
|
signatureURL: awsSignatureURL,
|
|
|
|
tokenURL: awsAPITokenURL,
|
|
|
|
tokenTTL: awsAPITokenTTL,
|
2020-10-14 00:51:24 +00:00
|
|
|
certificates: []*x509.Certificate{cert},
|
2020-07-22 23:52:06 +00:00
|
|
|
signatureAlgorithm: awsSignatureAlgorithm,
|
|
|
|
},
|
2022-03-10 02:43:45 +00:00
|
|
|
}
|
|
|
|
p.ctl, err = NewController(p, p.Claims, Config{
|
|
|
|
Audiences: testAudiences.WithFragment("aws/" + name),
|
|
|
|
})
|
|
|
|
return p, err
|
2020-07-22 23:52:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func generateAWSWithServerV1Only() (*AWS, *httptest.Server, error) {
|
|
|
|
aws, err := generateAWSV1Only()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
2019-04-25 02:52:58 +00:00
|
|
|
}
|
|
|
|
block, _ := pem.Decode([]byte(awsTestKey))
|
|
|
|
if block == nil || block.Type != "RSA PRIVATE KEY" {
|
|
|
|
return nil, nil, errors.New("error decoding AWS key")
|
|
|
|
}
|
|
|
|
key, err := x509.ParsePKCS1PrivateKey(block.Bytes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, errors.Wrap(err, "error parsing AWS private key")
|
|
|
|
}
|
|
|
|
doc, err := json.MarshalIndent(awsInstanceIdentityDocument{
|
|
|
|
AccountID: aws.Accounts[0],
|
|
|
|
Architecture: "x86_64",
|
|
|
|
AvailabilityZone: "us-west-2b",
|
2019-07-15 22:52:36 +00:00
|
|
|
ImageID: "image-id",
|
|
|
|
InstanceID: "instance-id",
|
2019-04-25 02:52:58 +00:00
|
|
|
InstanceType: "t2.micro",
|
|
|
|
PendingTime: time.Now(),
|
|
|
|
PrivateIP: "127.0.0.1",
|
|
|
|
Region: "us-west-1",
|
|
|
|
Version: "2017-09-30",
|
|
|
|
}, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
sum := sha256.Sum256(doc)
|
|
|
|
signature, err := key.Sign(rand.Reader, sum[:], crypto.SHA256)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, errors.Wrap(err, "error signing document")
|
|
|
|
}
|
|
|
|
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
switch r.URL.Path {
|
|
|
|
case "/latest/dynamic/instance-identity/document":
|
|
|
|
w.Write(doc)
|
|
|
|
case "/latest/dynamic/instance-identity/signature":
|
|
|
|
w.Write([]byte(base64.StdEncoding.EncodeToString(signature)))
|
|
|
|
case "/bad-document":
|
|
|
|
w.Write([]byte("{}"))
|
|
|
|
case "/bad-signature":
|
|
|
|
w.Write([]byte("YmFkLXNpZ25hdHVyZQo="))
|
2019-07-15 22:52:36 +00:00
|
|
|
case "/bad-json":
|
|
|
|
w.Write([]byte("{"))
|
2019-04-25 02:52:58 +00:00
|
|
|
default:
|
|
|
|
http.NotFound(w, r)
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
aws.config.identityURL = srv.URL + "/latest/dynamic/instance-identity/document"
|
|
|
|
aws.config.signatureURL = srv.URL + "/latest/dynamic/instance-identity/signature"
|
|
|
|
return aws, srv, nil
|
|
|
|
}
|
|
|
|
|
2019-05-08 02:07:49 +00:00
|
|
|
func generateAzure() (*Azure, error) {
|
|
|
|
name, err := randutil.Alphanumeric(10)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
tenantID, err := randutil.Alphanumeric(10)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
jwk, err := generateJSONWebKey()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-03-10 02:43:45 +00:00
|
|
|
p := &Azure{
|
2019-05-08 02:07:49 +00:00
|
|
|
Type: "Azure",
|
|
|
|
Name: name,
|
|
|
|
TenantID: tenantID,
|
2019-05-08 19:47:45 +00:00
|
|
|
Audience: azureDefaultAudience,
|
2019-05-08 02:07:49 +00:00
|
|
|
Claims: &globalProvisionerClaims,
|
|
|
|
config: newAzureConfig(tenantID),
|
|
|
|
oidcConfig: openIDConfiguration{
|
|
|
|
Issuer: "https://sts.windows.net/" + tenantID + "/",
|
|
|
|
JWKSetURI: "https://login.microsoftonline.com/common/discovery/keys",
|
|
|
|
},
|
|
|
|
keyStore: &keyStore{
|
|
|
|
keySet: jose.JSONWebKeySet{Keys: []jose.JSONWebKey{*jwk}},
|
|
|
|
expiry: time.Now().Add(24 * time.Hour),
|
|
|
|
},
|
2022-03-10 02:43:45 +00:00
|
|
|
}
|
|
|
|
p.ctl, err = NewController(p, p.Claims, Config{
|
|
|
|
Audiences: testAudiences,
|
|
|
|
})
|
|
|
|
return p, err
|
2019-05-08 02:07:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func generateAzureWithServer() (*Azure, *httptest.Server, error) {
|
|
|
|
az, err := generateAzure()
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
writeJSON := func(w http.ResponseWriter, v interface{}) {
|
|
|
|
b, err := json.Marshal(v)
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.Header().Add("Content-Type", "application/json")
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
w.Write(b)
|
|
|
|
}
|
|
|
|
getPublic := func(ks jose.JSONWebKeySet) jose.JSONWebKeySet {
|
|
|
|
var ret jose.JSONWebKeySet
|
|
|
|
for _, k := range ks.Keys {
|
|
|
|
ret.Keys = append(ret.Keys, k.Public())
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
issuer := "https://sts.windows.net/" + az.TenantID + "/"
|
|
|
|
srv := httptest.NewUnstartedServer(nil)
|
|
|
|
srv.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
switch r.URL.Path {
|
|
|
|
case "/error":
|
|
|
|
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
|
|
|
case "/" + az.TenantID + "/.well-known/openid-configuration":
|
|
|
|
writeJSON(w, openIDConfiguration{Issuer: issuer, JWKSetURI: srv.URL + "/jwks_uri"})
|
2019-05-08 19:47:45 +00:00
|
|
|
case "/openid-configuration-no-issuer":
|
|
|
|
writeJSON(w, openIDConfiguration{Issuer: "", JWKSetURI: srv.URL + "/jwks_uri"})
|
|
|
|
case "/openid-configuration-fail-jwk":
|
|
|
|
writeJSON(w, openIDConfiguration{Issuer: issuer, JWKSetURI: srv.URL + "/error"})
|
2019-05-08 02:07:49 +00:00
|
|
|
case "/random":
|
|
|
|
keySet := must(generateJSONWebKeySet(2))[0].(jose.JSONWebKeySet)
|
|
|
|
w.Header().Add("Cache-Control", "max-age=5")
|
|
|
|
writeJSON(w, getPublic(keySet))
|
|
|
|
case "/private":
|
|
|
|
writeJSON(w, az.keyStore.keySet)
|
|
|
|
case "/jwks_uri":
|
|
|
|
w.Header().Add("Cache-Control", "max-age=5")
|
|
|
|
writeJSON(w, getPublic(az.keyStore.keySet))
|
|
|
|
case "/metadata/identity/oauth2/token":
|
2022-03-07 11:24:58 +00:00
|
|
|
tok, err := generateAzureToken("subject", issuer, "https://management.azure.com/", az.TenantID, "subscriptionID", "resourceGroup", "virtualMachine", "vm", time.Now(), &az.keyStore.keySet.Keys[0])
|
2019-05-08 02:07:49 +00:00
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
} else {
|
|
|
|
writeJSON(w, azureIdentityToken{
|
|
|
|
AccessToken: tok,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
http.NotFound(w, r)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
srv.Start()
|
|
|
|
az.config.oidcDiscoveryURL = srv.URL + "/" + az.TenantID + "/.well-known/openid-configuration"
|
|
|
|
az.config.identityTokenURL = srv.URL + "/metadata/identity/oauth2/token"
|
|
|
|
return az, srv, nil
|
|
|
|
}
|
|
|
|
|
2019-03-08 03:30:17 +00:00
|
|
|
func generateCollection(nJWK, nOIDC int) (*Collection, error) {
|
|
|
|
col := NewCollection(testAudiences)
|
|
|
|
for i := 0; i < nJWK; i++ {
|
|
|
|
p, err := generateJWK()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
col.Store(p)
|
|
|
|
}
|
|
|
|
for i := 0; i < nOIDC; i++ {
|
|
|
|
p, err := generateOIDC()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
col.Store(p)
|
|
|
|
}
|
|
|
|
return col, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateSimpleToken(iss, aud string, jwk *jose.JSONWebKey) (string, error) {
|
2019-03-15 20:49:50 +00:00
|
|
|
return generateToken("subject", iss, aud, "name@smallstep.com", []string{"test.smallstep.com"}, time.Now(), jwk)
|
2019-03-09 01:24:58 +00:00
|
|
|
}
|
|
|
|
|
2019-09-05 01:31:09 +00:00
|
|
|
type tokOption func(*jose.SignerOptions) error
|
|
|
|
|
|
|
|
func withX5CHdr(certs []*x509.Certificate) tokOption {
|
|
|
|
return func(so *jose.SignerOptions) error {
|
|
|
|
strs := make([]string, len(certs))
|
|
|
|
for i, cert := range certs {
|
|
|
|
strs[i] = base64.StdEncoding.EncodeToString(cert.Raw)
|
|
|
|
}
|
|
|
|
so.WithHeader("x5c", strs)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-20 21:30:05 +00:00
|
|
|
func withSSHPOPFile(cert *ssh.Certificate) tokOption {
|
|
|
|
return func(so *jose.SignerOptions) error {
|
|
|
|
so.WithHeader("sshpop", base64.StdEncoding.EncodeToString(cert.Marshal()))
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-08 18:59:57 +00:00
|
|
|
func generateToken(sub, iss, aud, email string, sans []string, iat time.Time, jwk *jose.JSONWebKey, tokOpts ...tokOption) (string, error) {
|
2019-09-05 01:31:09 +00:00
|
|
|
so := new(jose.SignerOptions)
|
|
|
|
so.WithType("JWT")
|
|
|
|
so.WithHeader("kid", jwk.KeyID)
|
|
|
|
|
|
|
|
for _, o := range tokOpts {
|
|
|
|
if err := o(so); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sig, err := jose.NewSigner(jose.SigningKey{Algorithm: jose.ES256, Key: jwk.Key}, so)
|
2019-03-09 01:24:58 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
id, err := randutil.ASCII(64)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
claims := struct {
|
|
|
|
jose.Claims
|
2019-03-11 19:48:46 +00:00
|
|
|
Email string `json:"email"`
|
|
|
|
SANS []string `json:"sans"`
|
2019-03-09 01:24:58 +00:00
|
|
|
}{
|
|
|
|
Claims: jose.Claims{
|
|
|
|
ID: id,
|
|
|
|
Subject: sub,
|
|
|
|
Issuer: iss,
|
2019-03-11 19:48:46 +00:00
|
|
|
IssuedAt: jose.NewNumericDate(iat),
|
|
|
|
NotBefore: jose.NewNumericDate(iat),
|
|
|
|
Expiry: jose.NewNumericDate(iat.Add(5 * time.Minute)),
|
2019-03-09 01:24:58 +00:00
|
|
|
Audience: []string{aud},
|
|
|
|
},
|
2019-03-15 20:49:50 +00:00
|
|
|
Email: email,
|
2019-03-11 19:48:46 +00:00
|
|
|
SANS: sans,
|
2019-03-09 01:24:58 +00:00
|
|
|
}
|
|
|
|
return jose.Signed(sig).Claims(claims).CompactSerialize()
|
2019-03-08 03:30:17 +00:00
|
|
|
}
|
2019-03-11 17:40:55 +00:00
|
|
|
|
2021-10-08 18:59:57 +00:00
|
|
|
func generateOIDCToken(sub, iss, aud, email, preferredUsername string, iat time.Time, jwk *jose.JSONWebKey, tokOpts ...tokOption) (string, error) {
|
2021-04-30 01:30:26 +00:00
|
|
|
so := new(jose.SignerOptions)
|
|
|
|
so.WithType("JWT")
|
|
|
|
so.WithHeader("kid", jwk.KeyID)
|
|
|
|
|
|
|
|
for _, o := range tokOpts {
|
|
|
|
if err := o(so); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sig, err := jose.NewSigner(jose.SigningKey{Algorithm: jose.ES256, Key: jwk.Key}, so)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
id, err := randutil.ASCII(64)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
claims := struct {
|
|
|
|
jose.Claims
|
|
|
|
Email string `json:"email"`
|
|
|
|
PreferredUsername string `json:"preferred_username,omitempty"`
|
|
|
|
}{
|
|
|
|
Claims: jose.Claims{
|
|
|
|
ID: id,
|
|
|
|
Subject: sub,
|
|
|
|
Issuer: iss,
|
|
|
|
IssuedAt: jose.NewNumericDate(iat),
|
|
|
|
NotBefore: jose.NewNumericDate(iat),
|
|
|
|
Expiry: jose.NewNumericDate(iat.Add(5 * time.Minute)),
|
|
|
|
Audience: []string{aud},
|
|
|
|
},
|
|
|
|
Email: email,
|
|
|
|
PreferredUsername: preferredUsername,
|
|
|
|
}
|
|
|
|
return jose.Signed(sig).Claims(claims).CompactSerialize()
|
|
|
|
}
|
|
|
|
|
2019-12-20 21:30:05 +00:00
|
|
|
func generateX5CSSHToken(jwk *jose.JSONWebKey, claims *x5cPayload, tokOpts ...tokOption) (string, error) {
|
|
|
|
so := new(jose.SignerOptions)
|
|
|
|
so.WithType("JWT")
|
|
|
|
so.WithHeader("kid", jwk.KeyID)
|
|
|
|
|
|
|
|
for _, o := range tokOpts {
|
|
|
|
if err := o(so); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sig, err := jose.NewSigner(jose.SigningKey{Algorithm: jose.ES256, Key: jwk.Key}, so)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return jose.Signed(sig).Claims(claims).CompactSerialize()
|
|
|
|
}
|
|
|
|
|
2019-10-18 20:52:28 +00:00
|
|
|
func getK8sSAPayload() *k8sSAPayload {
|
|
|
|
return &k8sSAPayload{
|
|
|
|
Claims: jose.Claims{
|
|
|
|
Issuer: k8sSAIssuer,
|
|
|
|
Subject: "foo",
|
|
|
|
},
|
|
|
|
Namespace: "ns-foo",
|
|
|
|
SecretName: "sn-foo",
|
|
|
|
ServiceAccountName: "san-foo",
|
|
|
|
ServiceAccountUID: "sauid-foo",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateK8sSAToken(jwk *jose.JSONWebKey, claims *k8sSAPayload, tokOpts ...tokOption) (string, error) {
|
|
|
|
so := new(jose.SignerOptions)
|
|
|
|
so.WithHeader("kid", jwk.KeyID)
|
|
|
|
|
|
|
|
for _, o := range tokOpts {
|
|
|
|
if err := o(so); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sig, err := jose.NewSigner(jose.SigningKey{Algorithm: jose.ES256, Key: jwk.Key}, so)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
if claims == nil {
|
|
|
|
claims = getK8sSAPayload()
|
|
|
|
}
|
|
|
|
return jose.Signed(sig).Claims(*claims).CompactSerialize()
|
|
|
|
}
|
|
|
|
|
2019-07-31 01:23:54 +00:00
|
|
|
func generateSimpleSSHUserToken(iss, aud string, jwk *jose.JSONWebKey) (string, error) {
|
2020-07-23 01:24:45 +00:00
|
|
|
return generateSSHToken("subject@localhost", iss, aud, time.Now(), &SignSSHOptions{
|
2019-07-31 01:23:54 +00:00
|
|
|
CertType: "user",
|
|
|
|
Principals: []string{"name"},
|
|
|
|
}, jwk)
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateSimpleSSHHostToken(iss, aud string, jwk *jose.JSONWebKey) (string, error) {
|
2020-07-23 01:24:45 +00:00
|
|
|
return generateSSHToken("subject@localhost", iss, aud, time.Now(), &SignSSHOptions{
|
2019-07-31 01:23:54 +00:00
|
|
|
CertType: "host",
|
|
|
|
Principals: []string{"smallstep.com"},
|
|
|
|
}, jwk)
|
|
|
|
}
|
|
|
|
|
2020-07-23 01:24:45 +00:00
|
|
|
func generateSSHToken(sub, iss, aud string, iat time.Time, sshOpts *SignSSHOptions, jwk *jose.JSONWebKey) (string, error) {
|
2019-07-31 01:23:54 +00:00
|
|
|
sig, err := jose.NewSigner(
|
|
|
|
jose.SigningKey{Algorithm: jose.ES256, Key: jwk.Key},
|
|
|
|
new(jose.SignerOptions).WithType("JWT").WithHeader("kid", jwk.KeyID),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
id, err := randutil.ASCII(64)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
claims := struct {
|
|
|
|
jose.Claims
|
|
|
|
Step *stepPayload `json:"step,omitempty"`
|
|
|
|
}{
|
|
|
|
Claims: jose.Claims{
|
|
|
|
ID: id,
|
|
|
|
Subject: sub,
|
|
|
|
Issuer: iss,
|
|
|
|
IssuedAt: jose.NewNumericDate(iat),
|
|
|
|
NotBefore: jose.NewNumericDate(iat),
|
|
|
|
Expiry: jose.NewNumericDate(iat.Add(5 * time.Minute)),
|
|
|
|
Audience: []string{aud},
|
|
|
|
},
|
|
|
|
Step: &stepPayload{
|
|
|
|
SSH: sshOpts,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return jose.Signed(sig).Claims(claims).CompactSerialize()
|
|
|
|
}
|
|
|
|
|
2019-04-18 23:01:30 +00:00
|
|
|
func generateGCPToken(sub, iss, aud, instanceID, instanceName, projectID, zone string, iat time.Time, jwk *jose.JSONWebKey) (string, error) {
|
|
|
|
sig, err := jose.NewSigner(
|
|
|
|
jose.SigningKey{Algorithm: jose.ES256, Key: jwk.Key},
|
|
|
|
new(jose.SignerOptions).WithType("JWT").WithHeader("kid", jwk.KeyID),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2019-06-04 00:19:44 +00:00
|
|
|
aud, err = generateSignAudience("https://ca.smallstep.com", aud)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2019-04-18 23:01:30 +00:00
|
|
|
claims := gcpPayload{
|
|
|
|
Claims: jose.Claims{
|
|
|
|
Subject: sub,
|
|
|
|
Issuer: iss,
|
|
|
|
IssuedAt: jose.NewNumericDate(iat),
|
|
|
|
NotBefore: jose.NewNumericDate(iat),
|
|
|
|
Expiry: jose.NewNumericDate(iat.Add(5 * time.Minute)),
|
|
|
|
Audience: []string{aud},
|
|
|
|
},
|
|
|
|
AuthorizedParty: sub,
|
|
|
|
Email: "foo@developer.gserviceaccount.com",
|
|
|
|
EmailVerified: true,
|
|
|
|
Google: gcpGooglePayload{
|
|
|
|
ComputeEngine: gcpComputeEnginePayload{
|
|
|
|
InstanceID: instanceID,
|
|
|
|
InstanceName: instanceName,
|
2019-06-04 22:57:15 +00:00
|
|
|
InstanceCreationTimestamp: jose.NewNumericDate(iat),
|
2019-04-18 23:01:30 +00:00
|
|
|
ProjectID: projectID,
|
|
|
|
ProjectNumber: 1234567890,
|
|
|
|
Zone: zone,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return jose.Signed(sig).Claims(claims).CompactSerialize()
|
|
|
|
}
|
|
|
|
|
2020-12-17 21:35:14 +00:00
|
|
|
func generateAWSToken(p *AWS, sub, iss, aud, accountID, instanceID, privateIP, region string, iat time.Time, key crypto.Signer) (string, error) {
|
2019-04-25 02:52:58 +00:00
|
|
|
doc, err := json.MarshalIndent(awsInstanceIdentityDocument{
|
|
|
|
AccountID: accountID,
|
|
|
|
Architecture: "x86_64",
|
|
|
|
AvailabilityZone: "us-west-2b",
|
|
|
|
ImageID: "ami-123123",
|
|
|
|
InstanceID: instanceID,
|
|
|
|
InstanceType: "t2.micro",
|
2019-06-04 23:31:33 +00:00
|
|
|
PendingTime: iat,
|
2019-04-25 02:52:58 +00:00
|
|
|
PrivateIP: privateIP,
|
|
|
|
Region: region,
|
|
|
|
Version: "2017-09-30",
|
|
|
|
}, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
sum := sha256.Sum256(doc)
|
|
|
|
signature, err := key.Sign(rand.Reader, sum[:], crypto.SHA256)
|
|
|
|
if err != nil {
|
|
|
|
return "", errors.Wrap(err, "error signing document")
|
|
|
|
}
|
|
|
|
|
|
|
|
sig, err := jose.NewSigner(
|
|
|
|
jose.SigningKey{Algorithm: jose.HS256, Key: signature},
|
|
|
|
new(jose.SignerOptions).WithType("JWT"),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2019-06-06 20:09:00 +00:00
|
|
|
aud, err = generateSignAudience("https://ca.smallstep.com", aud)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2020-12-17 21:35:14 +00:00
|
|
|
unique := fmt.Sprintf("%s.%s", p.GetID(), instanceID)
|
|
|
|
sum = sha256.Sum256([]byte(unique))
|
|
|
|
|
2019-04-25 02:52:58 +00:00
|
|
|
claims := awsPayload{
|
|
|
|
Claims: jose.Claims{
|
2020-12-17 21:35:14 +00:00
|
|
|
ID: strings.ToLower(hex.EncodeToString(sum[:])),
|
2019-04-25 02:52:58 +00:00
|
|
|
Subject: sub,
|
|
|
|
Issuer: iss,
|
|
|
|
IssuedAt: jose.NewNumericDate(iat),
|
|
|
|
NotBefore: jose.NewNumericDate(iat),
|
|
|
|
Expiry: jose.NewNumericDate(iat.Add(5 * time.Minute)),
|
|
|
|
Audience: []string{aud},
|
|
|
|
},
|
|
|
|
Amazon: awsAmazonPayload{
|
|
|
|
Document: doc,
|
|
|
|
Signature: signature,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return jose.Signed(sig).Claims(claims).CompactSerialize()
|
|
|
|
}
|
|
|
|
|
2022-03-07 12:07:48 +00:00
|
|
|
func generateAzureToken(sub, iss, aud, tenantID, subscriptionID, resourceGroup, resourceName, resourceType string, iat time.Time, jwk *jose.JSONWebKey) (string, error) {
|
2019-05-08 02:07:49 +00:00
|
|
|
sig, err := jose.NewSigner(
|
|
|
|
jose.SigningKey{Algorithm: jose.ES256, Key: jwk.Key},
|
|
|
|
new(jose.SignerOptions).WithType("JWT").WithHeader("kid", jwk.KeyID),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2022-03-07 11:24:58 +00:00
|
|
|
var xmsMirID string
|
|
|
|
if resourceType == "vm" {
|
|
|
|
xmsMirID = fmt.Sprintf("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.Compute/virtualMachines/%s", subscriptionID, resourceGroup, resourceName)
|
|
|
|
} else if resourceType == "uai" {
|
|
|
|
xmsMirID = fmt.Sprintf("/subscriptions/%s/resourceGroups/%s/providers/Microsoft.ManagedIdentity/userAssignedIdentities/%s", subscriptionID, resourceGroup, resourceName)
|
|
|
|
}
|
2019-05-08 02:07:49 +00:00
|
|
|
|
|
|
|
claims := azurePayload{
|
|
|
|
Claims: jose.Claims{
|
|
|
|
Subject: sub,
|
|
|
|
Issuer: iss,
|
|
|
|
IssuedAt: jose.NewNumericDate(iat),
|
|
|
|
NotBefore: jose.NewNumericDate(iat),
|
|
|
|
Expiry: jose.NewNumericDate(iat.Add(5 * time.Minute)),
|
|
|
|
Audience: []string{aud},
|
2019-05-08 19:47:45 +00:00
|
|
|
ID: "the-jti",
|
2019-05-08 02:07:49 +00:00
|
|
|
},
|
|
|
|
AppID: "the-appid",
|
|
|
|
AppIDAcr: "the-appidacr",
|
|
|
|
IdentityProvider: "the-idp",
|
|
|
|
ObjectID: "the-oid",
|
|
|
|
TenantID: tenantID,
|
|
|
|
Version: "the-version",
|
2022-03-07 11:24:58 +00:00
|
|
|
XMSMirID: xmsMirID,
|
2019-05-08 02:07:49 +00:00
|
|
|
}
|
|
|
|
return jose.Signed(sig).Claims(claims).CompactSerialize()
|
|
|
|
}
|
|
|
|
|
2019-03-11 17:40:55 +00:00
|
|
|
func parseToken(token string) (*jose.JSONWebToken, *jose.Claims, error) {
|
|
|
|
tok, err := jose.ParseSigned(token)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
claims := new(jose.Claims)
|
|
|
|
if err := tok.UnsafeClaimsWithoutVerification(claims); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
return tok, claims, nil
|
|
|
|
}
|
|
|
|
|
2019-04-25 02:52:58 +00:00
|
|
|
func parseAWSToken(token string) (*jose.JSONWebToken, *awsPayload, error) {
|
|
|
|
tok, err := jose.ParseSigned(token)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
claims := new(awsPayload)
|
|
|
|
if err := tok.UnsafeClaimsWithoutVerification(claims); err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
var doc awsInstanceIdentityDocument
|
|
|
|
if err := json.Unmarshal(claims.Amazon.Document, &doc); err != nil {
|
|
|
|
return nil, nil, errors.Wrap(err, "error unmarshaling identity document")
|
|
|
|
}
|
|
|
|
claims.document = doc
|
|
|
|
return tok, claims, nil
|
|
|
|
}
|
|
|
|
|
2019-03-11 17:40:55 +00:00
|
|
|
func generateJWKServer(n int) *httptest.Server {
|
|
|
|
hits := struct {
|
|
|
|
Hits int `json:"hits"`
|
|
|
|
}{}
|
|
|
|
writeJSON := func(w http.ResponseWriter, v interface{}) {
|
|
|
|
b, err := json.Marshal(v)
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.Header().Add("Content-Type", "application/json")
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
w.Write(b)
|
|
|
|
}
|
2019-03-11 19:48:46 +00:00
|
|
|
getPublic := func(ks jose.JSONWebKeySet) jose.JSONWebKeySet {
|
|
|
|
var ret jose.JSONWebKeySet
|
|
|
|
for _, k := range ks.Keys {
|
|
|
|
ret.Keys = append(ret.Keys, k.Public())
|
|
|
|
}
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2019-06-24 20:40:14 +00:00
|
|
|
defaultKeySet := must(generateJSONWebKeySet(n))[0].(jose.JSONWebKeySet)
|
2019-03-11 19:48:46 +00:00
|
|
|
srv := httptest.NewUnstartedServer(nil)
|
|
|
|
srv.Config.Handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2019-03-11 17:40:55 +00:00
|
|
|
hits.Hits++
|
|
|
|
switch r.RequestURI {
|
|
|
|
case "/error":
|
|
|
|
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
|
|
|
case "/hits":
|
|
|
|
writeJSON(w, hits)
|
2019-09-18 23:04:43 +00:00
|
|
|
case "/.well-known/openid-configuration":
|
2019-03-11 19:48:46 +00:00
|
|
|
writeJSON(w, openIDConfiguration{Issuer: "the-issuer", JWKSetURI: srv.URL + "/jwks_uri"})
|
2020-04-24 21:36:32 +00:00
|
|
|
case "/common/.well-known/openid-configuration":
|
|
|
|
writeJSON(w, openIDConfiguration{Issuer: "https://login.microsoftonline.com/{tenantid}/v2.0", JWKSetURI: srv.URL + "/jwks_uri"})
|
2019-03-11 17:40:55 +00:00
|
|
|
case "/random":
|
2019-06-24 20:40:14 +00:00
|
|
|
keySet := must(generateJSONWebKeySet(n))[0].(jose.JSONWebKeySet)
|
2019-03-11 17:40:55 +00:00
|
|
|
w.Header().Add("Cache-Control", "max-age=5")
|
2019-03-11 19:48:46 +00:00
|
|
|
writeJSON(w, getPublic(keySet))
|
2019-06-24 20:40:14 +00:00
|
|
|
case "/no-cache":
|
|
|
|
keySet := must(generateJSONWebKeySet(n))[0].(jose.JSONWebKeySet)
|
|
|
|
w.Header().Add("Cache-Control", "no-cache, no-store, max-age=0, must-revalidate")
|
|
|
|
writeJSON(w, getPublic(keySet))
|
2019-03-11 19:48:46 +00:00
|
|
|
case "/private":
|
|
|
|
writeJSON(w, defaultKeySet)
|
2019-03-11 17:40:55 +00:00
|
|
|
default:
|
|
|
|
w.Header().Add("Cache-Control", "max-age=5")
|
2019-03-11 19:48:46 +00:00
|
|
|
writeJSON(w, getPublic(defaultKeySet))
|
2019-03-11 17:40:55 +00:00
|
|
|
}
|
2019-03-11 19:48:46 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
srv.Start()
|
|
|
|
return srv
|
2019-03-11 17:40:55 +00:00
|
|
|
}
|
2019-05-27 00:41:10 +00:00
|
|
|
|
|
|
|
func generateACME() (*ACME, error) {
|
|
|
|
// Initialize provisioners
|
|
|
|
p := &ACME{
|
|
|
|
Type: "ACME",
|
|
|
|
Name: "test@acme-provisioner.com",
|
|
|
|
}
|
|
|
|
if err := p.Init(Config{Claims: globalProvisionerClaims}); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return p, nil
|
|
|
|
}
|
2019-09-05 01:31:09 +00:00
|
|
|
|
|
|
|
func parseCerts(b []byte) ([]*x509.Certificate, error) {
|
|
|
|
var (
|
|
|
|
block *pem.Block
|
|
|
|
rest = b
|
|
|
|
certs = []*x509.Certificate{}
|
|
|
|
)
|
|
|
|
for rest != nil {
|
|
|
|
block, rest = pem.Decode(rest)
|
|
|
|
if block == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
cert, err := x509.ParseCertificate(block.Bytes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "error parsing x509 certificate from PEM block")
|
|
|
|
}
|
|
|
|
|
|
|
|
certs = append(certs, cert)
|
|
|
|
}
|
|
|
|
return certs, nil
|
|
|
|
}
|