certificates/authority/provisioner/aws_test.go

181 lines
6.5 KiB
Go

// +build ignore
package provisioner
import (
"crypto/x509"
"encoding/base64"
"encoding/pem"
"net/http"
"net/http/httptest"
"testing"
"github.com/fullsailor/pkcs7"
"github.com/smallstep/assert"
)
var rsaCert = `-----BEGIN CERTIFICATE-----
MIIDIjCCAougAwIBAgIJAKnL4UEDMN/FMA0GCSqGSIb3DQEBBQUAMGoxCzAJBgNV
BAYTAlVTMRMwEQYDVQQIEwpXYXNoaW5ndG9uMRAwDgYDVQQHEwdTZWF0dGxlMRgw
FgYDVQQKEw9BbWF6b24uY29tIEluYy4xGjAYBgNVBAMTEWVjMi5hbWF6b25hd3Mu
Y29tMB4XDTE0MDYwNTE0MjgwMloXDTI0MDYwNTE0MjgwMlowajELMAkGA1UEBhMC
VVMxEzARBgNVBAgTCldhc2hpbmd0b24xEDAOBgNVBAcTB1NlYXR0bGUxGDAWBgNV
BAoTD0FtYXpvbi5jb20gSW5jLjEaMBgGA1UEAxMRZWMyLmFtYXpvbmF3cy5jb20w
gZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBAIe9GN//SRK2knbjySG0ho3yqQM3
e2TDhWO8D2e8+XZqck754gFSo99AbT2RmXClambI7xsYHZFapbELC4H91ycihvrD
jbST1ZjkLQgga0NE1q43eS68ZeTDccScXQSNivSlzJZS8HJZjgqzBlXjZftjtdJL
XeE4hwvo0sD4f3j9AgMBAAGjgc8wgcwwHQYDVR0OBBYEFCXWzAgVyrbwnFncFFIs
77VBdlE4MIGcBgNVHSMEgZQwgZGAFCXWzAgVyrbwnFncFFIs77VBdlE4oW6kbDBq
MQswCQYDVQQGEwJVUzETMBEGA1UECBMKV2FzaGluZ3RvbjEQMA4GA1UEBxMHU2Vh
dHRsZTEYMBYGA1UEChMPQW1hem9uLmNvbSBJbmMuMRowGAYDVQQDExFlYzIuYW1h
em9uYXdzLmNvbYIJAKnL4UEDMN/FMAwGA1UdEwQFMAMBAf8wDQYJKoZIhvcNAQEF
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ICJ1cy13
ZXN0LTEiCn0AAAAAAAAxggEYMIIBFAIBATBpMFwxCzAJBgNVBAYTAlVTMRkwFwYDVQQIExBXYXNo
aW5ndG9uIFN0YXRlMRAwDgYDVQQHEwdTZWF0dGxlMSAwHgYDVQQKExdBbWF6b24gV2ViIFNlcnZp
Y2VzIExMQwIJAJa6SNnlXhpnMAkGBSsOAwIaBQCgXTAYBgkqhkiG9w0BCQMxCwYJKoZIhvcNAQcB
MBwGCSqGSIb3DQEJBTEPFw0xODA3MzAyMzMxMDRaMCMGCSqGSIb3DQEJBDEWBBQUze548OLd+uOT
aOSTDLlV9mevbTAJBgcqhkjOOAQDBC8wLQIUDGeP44Ge1atMQghe+ENV4IDM0zQCFQCBTOEvfKu+
uscwutj+7RCNgSVaWgAAAAAAAA==`
var doc = `{
"privateIp" : "172.31.23.47",
"devpayProductCodes" : null,
"marketplaceProductCodes" : null,
"version" : "2017-09-30",
"instanceId" : "i-02e3bec1f600f5e33",
"billingProducts" : null,
"instanceType" : "t2.micro",
"availabilityZone" : "us-west-1b",
"kernelId" : null,
"ramdiskId" : null,
"accountId" : "807492473263",
"architecture" : "x86_64",
"imageId" : "ami-1c1d217c",
"pendingTime" : "2017-11-21T00:25:23Z",
"region" : "us-west-1"
}`
func TestAWSRSA(t *testing.T) {
block, _ := pem.Decode([]byte(rsaCert))
cert, err := x509.ParseCertificate(block.Bytes)
assert.FatalError(t, err)
signature, err := base64.StdEncoding.DecodeString(rsaSig)
assert.FatalError(t, err)
err = cert.CheckSignature(x509.SHA256WithRSA, []byte(doc), signature)
assert.FatalError(t, err)
}
func TestAWSDSA(t *testing.T) {
block, _ := pem.Decode([]byte(dsaCert))
cert, err := x509.ParseCertificate(block.Bytes)
assert.FatalError(t, err)
signature, err := base64.StdEncoding.DecodeString(dsaSig)
assert.FatalError(t, err)
p7, err := pkcs7.Parse(signature)
assert.FatalError(t, err)
p7.Certificates = append(p7.Certificates, cert)
assert.FatalError(t, p7.Verify())
}
func TestAWS_GetIdentityToken(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/document":
w.Write([]byte(doc))
case "/signature":
w.Write([]byte(rsaSig))
default:
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
}
}))
defer srv.Close()
config, err := newAWSConfig()
assert.FatalError(t, err)
config.identityURL = srv.URL + "/document"
config.signatureURL = srv.URL + "/signature"
type fields struct {
Type string
Name string
Claims *Claims
claimer *Claimer
config *awsConfig
}
tests := []struct {
name string
fields fields
want string
wantErr bool
}{
{"ok", fields{"AWS", "name", nil, nil, config}, "", false},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
p := &AWS{
Type: tt.fields.Type,
Name: tt.fields.Name,
Claims: tt.fields.Claims,
claimer: tt.fields.claimer,
config: tt.fields.config,
}
got, err := p.GetIdentityToken()
if (err != nil) != tt.wantErr {
t.Errorf("AWS.GetIdentityToken() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("AWS.GetIdentityToken() = %v, want %v", got, tt.want)
}
t.Error(got)
// parts := strings.Split(got, ".")
// signed, err := base64.RawURLEncoding.DecodeString(parts[0])
// assert.FatalError(t, err)
// signature, err := base64.RawURLEncoding.DecodeString(parts[1])
// assert.FatalError(t, err)
// assert.FatalError(t, err, config.certificate.CheckSignature(config.signatureAlgorithm, signed, signature))
})
}
}