2019-04-24 19:12:36 +00:00
|
|
|
package provisioner
|
|
|
|
|
|
|
|
import (
|
2019-07-29 22:54:07 +00:00
|
|
|
"context"
|
2019-04-24 19:12:36 +00:00
|
|
|
"crypto/sha256"
|
|
|
|
"crypto/x509"
|
|
|
|
"encoding/base64"
|
|
|
|
"encoding/hex"
|
|
|
|
"encoding/json"
|
|
|
|
"encoding/pem"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2019-12-20 21:30:05 +00:00
|
|
|
"github.com/smallstep/certificates/errs"
|
2019-04-24 19:12:36 +00:00
|
|
|
"github.com/smallstep/cli/jose"
|
|
|
|
)
|
|
|
|
|
|
|
|
// awsIssuer is the string used as issuer in the generated tokens.
|
|
|
|
const awsIssuer = "ec2.amazonaws.com"
|
|
|
|
|
|
|
|
// awsIdentityURL is the url used to retrieve the instance identity document.
|
|
|
|
const awsIdentityURL = "http://169.254.169.254/latest/dynamic/instance-identity/document"
|
|
|
|
|
|
|
|
// awsSignatureURL is the url used to retrieve the instance identity signature.
|
|
|
|
const awsSignatureURL = "http://169.254.169.254/latest/dynamic/instance-identity/signature"
|
|
|
|
|
|
|
|
// awsCertificate is the certificate used to validate the instance identity
|
|
|
|
// signature.
|
|
|
|
const awsCertificate = `-----BEGIN CERTIFICATE-----
|
|
|
|
MIIDIjCCAougAwIBAgIJAKnL4UEDMN/FMA0GCSqGSIb3DQEBBQUAMGoxCzAJBgNV
|
|
|
|
BAYTAlVTMRMwEQYDVQQIEwpXYXNoaW5ndG9uMRAwDgYDVQQHEwdTZWF0dGxlMRgw
|
|
|
|
FgYDVQQKEw9BbWF6b24uY29tIEluYy4xGjAYBgNVBAMTEWVjMi5hbWF6b25hd3Mu
|
|
|
|
Y29tMB4XDTE0MDYwNTE0MjgwMloXDTI0MDYwNTE0MjgwMlowajELMAkGA1UEBhMC
|
|
|
|
VVMxEzARBgNVBAgTCldhc2hpbmd0b24xEDAOBgNVBAcTB1NlYXR0bGUxGDAWBgNV
|
|
|
|
BAoTD0FtYXpvbi5jb20gSW5jLjEaMBgGA1UEAxMRZWMyLmFtYXpvbmF3cy5jb20w
|
|
|
|
gZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBAIe9GN//SRK2knbjySG0ho3yqQM3
|
|
|
|
e2TDhWO8D2e8+XZqck754gFSo99AbT2RmXClambI7xsYHZFapbELC4H91ycihvrD
|
|
|
|
jbST1ZjkLQgga0NE1q43eS68ZeTDccScXQSNivSlzJZS8HJZjgqzBlXjZftjtdJL
|
|
|
|
XeE4hwvo0sD4f3j9AgMBAAGjgc8wgcwwHQYDVR0OBBYEFCXWzAgVyrbwnFncFFIs
|
|
|
|
77VBdlE4MIGcBgNVHSMEgZQwgZGAFCXWzAgVyrbwnFncFFIs77VBdlE4oW6kbDBq
|
|
|
|
MQswCQYDVQQGEwJVUzETMBEGA1UECBMKV2FzaGluZ3RvbjEQMA4GA1UEBxMHU2Vh
|
|
|
|
dHRsZTEYMBYGA1UEChMPQW1hem9uLmNvbSBJbmMuMRowGAYDVQQDExFlYzIuYW1h
|
|
|
|
em9uYXdzLmNvbYIJAKnL4UEDMN/FMAwGA1UdEwQFMAMBAf8wDQYJKoZIhvcNAQEF
|
|
|
|
BQADgYEAFYcz1OgEhQBXIwIdsgCOS8vEtiJYF+j9uO6jz7VOmJqO+pRlAbRlvY8T
|
|
|
|
C1haGgSI/A1uZUKs/Zfnph0oEI0/hu1IIJ/SKBDtN5lvmZ/IzbOPIJWirlsllQIQ
|
|
|
|
7zvWbGd9c9+Rm3p04oTvhup99la7kZqevJK0QRdD/6NpCKsqP/0=
|
|
|
|
-----END CERTIFICATE-----`
|
|
|
|
|
|
|
|
// awsSignatureAlgorithm is the signature algorithm used to verify the identity
|
|
|
|
// document signature.
|
|
|
|
const awsSignatureAlgorithm = x509.SHA256WithRSA
|
|
|
|
|
|
|
|
type awsConfig struct {
|
|
|
|
identityURL string
|
|
|
|
signatureURL string
|
|
|
|
certificate *x509.Certificate
|
|
|
|
signatureAlgorithm x509.SignatureAlgorithm
|
|
|
|
}
|
|
|
|
|
|
|
|
func newAWSConfig() (*awsConfig, error) {
|
|
|
|
block, _ := pem.Decode([]byte(awsCertificate))
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
return &awsConfig{
|
|
|
|
identityURL: awsIdentityURL,
|
|
|
|
signatureURL: awsSignatureURL,
|
|
|
|
certificate: cert,
|
|
|
|
signatureAlgorithm: awsSignatureAlgorithm,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type awsPayload struct {
|
|
|
|
jose.Claims
|
|
|
|
Amazon awsAmazonPayload `json:"amazon"`
|
|
|
|
SANs []string `json:"sans"`
|
|
|
|
document awsInstanceIdentityDocument
|
|
|
|
}
|
|
|
|
|
|
|
|
type awsAmazonPayload struct {
|
|
|
|
Document []byte `json:"document"`
|
|
|
|
Signature []byte `json:"signature"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type awsInstanceIdentityDocument struct {
|
|
|
|
AccountID string `json:"accountId"`
|
|
|
|
Architecture string `json:"architecture"`
|
|
|
|
AvailabilityZone string `json:"availabilityZone"`
|
|
|
|
BillingProducts []string `json:"billingProducts"`
|
|
|
|
DevpayProductCodes []string `json:"devpayProductCodes"`
|
|
|
|
ImageID string `json:"imageId"`
|
|
|
|
InstanceID string `json:"instanceId"`
|
|
|
|
InstanceType string `json:"instanceType"`
|
|
|
|
KernelID string `json:"kernelId"`
|
|
|
|
PendingTime time.Time `json:"pendingTime"`
|
|
|
|
PrivateIP string `json:"privateIp"`
|
|
|
|
RamdiskID string `json:"ramdiskId"`
|
|
|
|
Region string `json:"region"`
|
|
|
|
Version string `json:"version"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// AWS is the provisioner that supports identity tokens created from the Amazon
|
|
|
|
// Web Services Instance Identity Documents.
|
|
|
|
//
|
|
|
|
// If DisableCustomSANs is true, only the internal DNS and IP will be added as a
|
|
|
|
// SAN. By default it will accept any SAN in the CSR.
|
|
|
|
//
|
|
|
|
// If DisableTrustOnFirstUse is true, multiple sign request for this provisioner
|
|
|
|
// with the same instance will be accepted. By default only the first request
|
|
|
|
// will be accepted.
|
2019-06-05 18:04:00 +00:00
|
|
|
//
|
2019-06-07 18:24:56 +00:00
|
|
|
// If InstanceAge is set, only the instances with a pendingTime within the given
|
|
|
|
// period will be accepted.
|
2019-06-05 18:04:00 +00:00
|
|
|
//
|
|
|
|
// Amazon Identity docs are available at
|
|
|
|
// https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instance-identity-documents.html
|
2019-04-24 19:12:36 +00:00
|
|
|
type AWS struct {
|
2019-10-28 18:50:43 +00:00
|
|
|
*base
|
2019-04-24 19:12:36 +00:00
|
|
|
Type string `json:"type"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
Accounts []string `json:"accounts"`
|
|
|
|
DisableCustomSANs bool `json:"disableCustomSANs"`
|
|
|
|
DisableTrustOnFirstUse bool `json:"disableTrustOnFirstUse"`
|
2019-06-04 23:31:33 +00:00
|
|
|
InstanceAge Duration `json:"instanceAge,omitempty"`
|
2019-04-24 20:05:46 +00:00
|
|
|
Claims *Claims `json:"claims,omitempty"`
|
2019-04-24 19:12:36 +00:00
|
|
|
claimer *Claimer
|
|
|
|
config *awsConfig
|
2019-06-06 19:49:51 +00:00
|
|
|
audiences Audiences
|
2019-04-24 19:12:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetID returns the provisioner unique identifier.
|
|
|
|
func (p *AWS) GetID() string {
|
2019-06-06 19:49:51 +00:00
|
|
|
return "aws/" + p.Name
|
2019-04-24 19:12:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetTokenID returns the identifier of the token.
|
|
|
|
func (p *AWS) GetTokenID(token string) (string, error) {
|
|
|
|
payload, err := p.authorizeToken(token)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
// If TOFU is disabled create an ID for the token, so it cannot be reused.
|
|
|
|
// The timestamps, document and signatures should be mostly unique.
|
|
|
|
if p.DisableTrustOnFirstUse {
|
|
|
|
sum := sha256.Sum256([]byte(token))
|
|
|
|
return strings.ToLower(hex.EncodeToString(sum[:])), nil
|
|
|
|
}
|
|
|
|
return payload.ID, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetName returns the name of the provisioner.
|
|
|
|
func (p *AWS) GetName() string {
|
|
|
|
return p.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetType returns the type of provisioner.
|
|
|
|
func (p *AWS) GetType() Type {
|
|
|
|
return TypeAWS
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetEncryptedKey is not available in an AWS provisioner.
|
|
|
|
func (p *AWS) GetEncryptedKey() (kid string, key string, ok bool) {
|
|
|
|
return "", "", false
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetIdentityToken retrieves the identity document and it's signature and
|
|
|
|
// generates a token with them.
|
2019-07-15 22:52:36 +00:00
|
|
|
func (p *AWS) GetIdentityToken(subject, caURL string) (string, error) {
|
2019-04-24 19:12:36 +00:00
|
|
|
// Initialize the config if this method is used from the cli.
|
|
|
|
if err := p.assertConfig(); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
var idoc awsInstanceIdentityDocument
|
|
|
|
doc, err := p.readURL(p.config.identityURL)
|
|
|
|
if err != nil {
|
|
|
|
return "", errors.Wrap(err, "error retrieving identity document, are you in an AWS VM?")
|
|
|
|
}
|
|
|
|
if err := json.Unmarshal(doc, &idoc); err != nil {
|
|
|
|
return "", errors.Wrap(err, "error unmarshaling identity document")
|
|
|
|
}
|
|
|
|
sig, err := p.readURL(p.config.signatureURL)
|
|
|
|
if err != nil {
|
|
|
|
return "", errors.Wrap(err, "error retrieving identity document signature, are you in an AWS VM?")
|
|
|
|
}
|
|
|
|
signature, err := base64.StdEncoding.DecodeString(string(sig))
|
|
|
|
if err != nil {
|
|
|
|
return "", errors.Wrap(err, "error decoding identity document signature")
|
|
|
|
}
|
|
|
|
if err := p.checkSignature(doc, signature); err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2019-06-06 19:49:51 +00:00
|
|
|
audience, err := generateSignAudience(caURL, p.GetID())
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
2019-04-24 19:12:36 +00:00
|
|
|
// Create unique ID for Trust On First Use (TOFU). Only the first instance
|
|
|
|
// per provisioner is allowed as we don't have a way to trust the given
|
|
|
|
// sans.
|
2019-04-24 20:05:46 +00:00
|
|
|
unique := fmt.Sprintf("%s.%s", p.GetID(), idoc.InstanceID)
|
2019-04-24 19:12:36 +00:00
|
|
|
sum := sha256.Sum256([]byte(unique))
|
|
|
|
|
|
|
|
// Create a JWT from the identity document
|
|
|
|
signer, err := jose.NewSigner(
|
|
|
|
jose.SigningKey{Algorithm: jose.HS256, Key: signature},
|
|
|
|
new(jose.SignerOptions).WithType("JWT"),
|
|
|
|
)
|
2019-04-24 21:59:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", errors.Wrap(err, "error creating signer")
|
|
|
|
}
|
2019-04-24 19:12:36 +00:00
|
|
|
|
|
|
|
now := time.Now()
|
|
|
|
payload := awsPayload{
|
|
|
|
Claims: jose.Claims{
|
|
|
|
Issuer: awsIssuer,
|
2019-07-15 22:52:36 +00:00
|
|
|
Subject: subject,
|
2019-06-06 19:49:51 +00:00
|
|
|
Audience: []string{audience},
|
2019-04-24 19:12:36 +00:00
|
|
|
Expiry: jose.NewNumericDate(now.Add(5 * time.Minute)),
|
|
|
|
NotBefore: jose.NewNumericDate(now),
|
|
|
|
IssuedAt: jose.NewNumericDate(now),
|
|
|
|
ID: strings.ToLower(hex.EncodeToString(sum[:])),
|
|
|
|
},
|
|
|
|
Amazon: awsAmazonPayload{
|
|
|
|
Document: doc,
|
|
|
|
Signature: signature,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
tok, err := jose.Signed(signer).Claims(payload).CompactSerialize()
|
|
|
|
if err != nil {
|
|
|
|
return "", errors.Wrap(err, "error serialiazing token")
|
|
|
|
}
|
|
|
|
|
|
|
|
return tok, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Init validates and initializes the AWS provisioner.
|
|
|
|
func (p *AWS) Init(config Config) (err error) {
|
|
|
|
switch {
|
|
|
|
case p.Type == "":
|
|
|
|
return errors.New("provisioner type cannot be empty")
|
|
|
|
case p.Name == "":
|
|
|
|
return errors.New("provisioner name cannot be empty")
|
2019-06-04 23:31:33 +00:00
|
|
|
case p.InstanceAge.Value() < 0:
|
|
|
|
return errors.New("provisioner instanceAge cannot be negative")
|
2019-04-24 19:12:36 +00:00
|
|
|
}
|
|
|
|
// Update claims with global ones
|
|
|
|
if p.claimer, err = NewClaimer(p.Claims, config.Claims); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Add default config
|
|
|
|
if p.config, err = newAWSConfig(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-06-06 19:49:51 +00:00
|
|
|
p.audiences = config.Audiences.WithFragment(p.GetID())
|
2019-04-24 19:12:36 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// AuthorizeSign validates the given token and returns the sign options that
|
|
|
|
// will be used on certificate creation.
|
2019-07-29 22:54:07 +00:00
|
|
|
func (p *AWS) AuthorizeSign(ctx context.Context, token string) ([]SignOption, error) {
|
2019-04-24 19:12:36 +00:00
|
|
|
payload, err := p.authorizeToken(token)
|
|
|
|
if err != nil {
|
2019-12-20 21:30:05 +00:00
|
|
|
return nil, errs.Wrap(http.StatusInternalServerError, err, "aws.AuthorizeSign")
|
2019-04-24 19:12:36 +00:00
|
|
|
}
|
|
|
|
|
2019-07-29 22:54:07 +00:00
|
|
|
doc := payload.document
|
2019-07-15 22:52:36 +00:00
|
|
|
// Enforce known CN and default DNS and IP if configured.
|
|
|
|
// By default we'll accept the CN and SANs in the CSR.
|
2019-04-24 20:05:46 +00:00
|
|
|
// There's no way to trust them other than TOFU.
|
2019-04-24 19:12:36 +00:00
|
|
|
var so []SignOption
|
|
|
|
if p.DisableCustomSANs {
|
|
|
|
so = append(so, dnsNamesValidator([]string{
|
|
|
|
fmt.Sprintf("ip-%s.%s.compute.internal", strings.Replace(doc.PrivateIP, ".", "-", -1), doc.Region),
|
|
|
|
}))
|
|
|
|
so = append(so, ipAddressesValidator([]net.IP{
|
|
|
|
net.ParseIP(doc.PrivateIP),
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
|
|
|
return append(so,
|
2019-09-05 01:31:09 +00:00
|
|
|
// modifiers / withOptions
|
|
|
|
newProvisionerExtensionOption(TypeAWS, p.Name, doc.AccountID, "InstanceID", doc.InstanceID),
|
|
|
|
profileDefaultDuration(p.claimer.DefaultTLSCertDuration()),
|
|
|
|
// validators
|
2019-08-27 00:52:49 +00:00
|
|
|
defaultPublicKeyValidator{},
|
2019-07-15 22:52:36 +00:00
|
|
|
commonNameValidator(payload.Claims.Subject),
|
2019-04-24 19:12:36 +00:00
|
|
|
newValidityValidator(p.claimer.MinTLSCertDuration(), p.claimer.MaxTLSCertDuration()),
|
|
|
|
), nil
|
|
|
|
}
|
|
|
|
|
2019-10-28 18:50:43 +00:00
|
|
|
// AuthorizeRenew returns an error if the renewal is disabled.
|
|
|
|
// NOTE: This method does not actually validate the certificate or check it's
|
|
|
|
// revocation status. Just confirms that the provisioner that created the
|
|
|
|
// certificate was configured to allow renewals.
|
|
|
|
func (p *AWS) AuthorizeRenew(ctx context.Context, cert *x509.Certificate) error {
|
2019-04-24 19:12:36 +00:00
|
|
|
if p.claimer.IsDisableRenewal() {
|
2020-01-24 06:04:34 +00:00
|
|
|
return errs.Unauthorized("aws.AuthorizeRenew; renew is disabled for aws provisioner %s", p.GetID())
|
2019-04-24 19:12:36 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// assertConfig initializes the config if it has not been initialized
|
|
|
|
func (p *AWS) assertConfig() (err error) {
|
|
|
|
if p.config != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
p.config, err = newAWSConfig()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// checkSignature returns an error if the signature is not valid.
|
|
|
|
func (p *AWS) checkSignature(signed, signature []byte) error {
|
|
|
|
if err := p.config.certificate.CheckSignature(p.config.signatureAlgorithm, signed, signature); err != nil {
|
|
|
|
return errors.Wrap(err, "error validating identity document signature")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// readURL does a GET request to the given url and returns the body. It's not
|
|
|
|
// using pkg/errors to avoid verbose errors, the caller should use it and write
|
|
|
|
// the appropriate error.
|
|
|
|
func (p *AWS) readURL(url string) ([]byte, error) {
|
|
|
|
r, err := http.Get(url)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer r.Body.Close()
|
|
|
|
b, err := ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return b, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// authorizeToken performs common jwt authorization actions and returns the
|
|
|
|
// claims for case specific downstream parsing.
|
|
|
|
// e.g. a Sign request will auth/validate different fields than a Revoke request.
|
|
|
|
func (p *AWS) authorizeToken(token string) (*awsPayload, error) {
|
|
|
|
jwt, err := jose.ParseSigned(token)
|
|
|
|
if err != nil {
|
2019-12-20 21:30:05 +00:00
|
|
|
return nil, errs.Wrapf(http.StatusUnauthorized, err, "aws.authorizeToken; error parsing aws token")
|
2019-04-24 19:12:36 +00:00
|
|
|
}
|
|
|
|
if len(jwt.Headers) == 0 {
|
2020-01-24 06:04:34 +00:00
|
|
|
return nil, errs.InternalServer("aws.authorizeToken; error parsing token, header is missing")
|
2019-04-24 19:12:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var unsafeClaims awsPayload
|
|
|
|
if err := jwt.UnsafeClaimsWithoutVerification(&unsafeClaims); err != nil {
|
2019-12-20 21:30:05 +00:00
|
|
|
return nil, errs.Wrap(http.StatusUnauthorized, err, "aws.authorizeToken; error unmarshaling claims")
|
2019-04-24 19:12:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var payload awsPayload
|
|
|
|
if err := jwt.Claims(unsafeClaims.Amazon.Signature, &payload); err != nil {
|
2019-12-20 21:30:05 +00:00
|
|
|
return nil, errs.Wrap(http.StatusUnauthorized, err, "aws.authorizeToken; error verifying claims")
|
2019-04-24 19:12:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Validate identity document signature
|
|
|
|
if err := p.checkSignature(payload.Amazon.Document, payload.Amazon.Signature); err != nil {
|
2019-12-20 21:30:05 +00:00
|
|
|
return nil, errs.Wrap(http.StatusUnauthorized, err, "aws.authorizeToken; invalid aws token signature")
|
2019-04-24 19:12:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var doc awsInstanceIdentityDocument
|
|
|
|
if err := json.Unmarshal(payload.Amazon.Document, &doc); err != nil {
|
2019-12-20 21:30:05 +00:00
|
|
|
return nil, errs.Wrap(http.StatusUnauthorized, err, "aws.authorizeToken; error unmarshaling aws identity document")
|
2019-04-24 19:12:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case doc.AccountID == "":
|
2020-01-24 06:04:34 +00:00
|
|
|
return nil, errs.Unauthorized("aws.authorizeToken; aws identity document accountId cannot be empty")
|
2019-04-24 19:12:36 +00:00
|
|
|
case doc.InstanceID == "":
|
2020-01-24 06:04:34 +00:00
|
|
|
return nil, errs.Unauthorized("aws.authorizeToken; aws identity document instanceId cannot be empty")
|
2019-04-24 19:12:36 +00:00
|
|
|
case doc.PrivateIP == "":
|
2020-01-24 06:04:34 +00:00
|
|
|
return nil, errs.Unauthorized("aws.authorizeToken; aws identity document privateIp cannot be empty")
|
2019-04-24 19:12:36 +00:00
|
|
|
case doc.Region == "":
|
2020-01-24 06:04:34 +00:00
|
|
|
return nil, errs.Unauthorized("aws.authorizeToken; aws identity document region cannot be empty")
|
2019-04-24 19:12:36 +00:00
|
|
|
}
|
|
|
|
|
2019-04-25 02:52:58 +00:00
|
|
|
// According to "rfc7519 JSON Web Token" acceptable skew should be no
|
|
|
|
// more than a few minutes.
|
2019-06-04 23:31:33 +00:00
|
|
|
now := time.Now().UTC()
|
2019-04-25 02:52:58 +00:00
|
|
|
if err = payload.ValidateWithLeeway(jose.Expected{
|
2019-07-15 22:52:36 +00:00
|
|
|
Issuer: awsIssuer,
|
|
|
|
Time: now,
|
2019-04-25 02:52:58 +00:00
|
|
|
}, time.Minute); err != nil {
|
2019-12-20 21:30:05 +00:00
|
|
|
return nil, errs.Wrapf(http.StatusUnauthorized, err, "aws.authorizeToken; invalid aws token")
|
2019-04-25 02:52:58 +00:00
|
|
|
}
|
|
|
|
|
2019-06-06 19:49:51 +00:00
|
|
|
// validate audiences with the defaults
|
|
|
|
if !matchesAudience(payload.Audience, p.audiences.Sign) {
|
2020-01-24 06:04:34 +00:00
|
|
|
return nil, errs.Unauthorized("aws.authorizeToken; invalid token - invalid audience claim (aud)")
|
2019-06-06 19:49:51 +00:00
|
|
|
}
|
|
|
|
|
2019-07-15 22:52:36 +00:00
|
|
|
// Validate subject, it has to be known if disableCustomSANs is enabled
|
|
|
|
if p.DisableCustomSANs {
|
|
|
|
if payload.Subject != doc.InstanceID &&
|
|
|
|
payload.Subject != doc.PrivateIP &&
|
|
|
|
payload.Subject != fmt.Sprintf("ip-%s.%s.compute.internal", strings.Replace(doc.PrivateIP, ".", "-", -1), doc.Region) {
|
2020-01-24 06:04:34 +00:00
|
|
|
return nil, errs.Unauthorized("aws.authorizeToken; invalid token - invalid subject claim (sub)")
|
2019-07-15 22:52:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-24 19:12:36 +00:00
|
|
|
// validate accounts
|
|
|
|
if len(p.Accounts) > 0 {
|
|
|
|
var found bool
|
|
|
|
for _, sa := range p.Accounts {
|
|
|
|
if sa == doc.AccountID {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
2020-01-24 06:04:34 +00:00
|
|
|
return nil, errs.Unauthorized("aws.authorizeToken; invalid aws identity document - accountId is not valid")
|
2019-04-24 19:12:36 +00:00
|
|
|
}
|
|
|
|
}
|
2019-06-04 23:31:33 +00:00
|
|
|
|
|
|
|
// validate instance age
|
|
|
|
if d := p.InstanceAge.Value(); d > 0 {
|
|
|
|
if now.Sub(doc.PendingTime) > d {
|
2020-01-24 06:04:34 +00:00
|
|
|
return nil, errs.Unauthorized("aws.authorizeToken; aws identity document pendingTime is too old")
|
2019-06-04 23:31:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-24 19:12:36 +00:00
|
|
|
payload.document = doc
|
|
|
|
return &payload, nil
|
|
|
|
}
|
2019-07-29 22:54:07 +00:00
|
|
|
|
2019-10-28 18:50:43 +00:00
|
|
|
// AuthorizeSSHSign returns the list of SignOption for a SignSSH request.
|
|
|
|
func (p *AWS) AuthorizeSSHSign(ctx context.Context, token string) ([]SignOption, error) {
|
|
|
|
if !p.claimer.IsSSHCAEnabled() {
|
2020-01-24 06:04:34 +00:00
|
|
|
return nil, errs.Unauthorized("aws.AuthorizeSSHSign; ssh ca is disabled for aws provisioner %s", p.GetID())
|
2019-10-28 18:50:43 +00:00
|
|
|
}
|
|
|
|
claims, err := p.authorizeToken(token)
|
|
|
|
if err != nil {
|
2019-12-20 21:30:05 +00:00
|
|
|
return nil, errs.Wrap(http.StatusInternalServerError, err, "aws.AuthorizeSSHSign")
|
2019-10-28 18:50:43 +00:00
|
|
|
}
|
|
|
|
|
2019-07-30 00:54:38 +00:00
|
|
|
doc := claims.document
|
|
|
|
|
|
|
|
signOptions := []SignOption{
|
2020-03-05 22:33:42 +00:00
|
|
|
// set the key id to the instance id
|
|
|
|
sshCertKeyIDModifier(doc.InstanceID),
|
2019-07-30 00:54:38 +00:00
|
|
|
}
|
|
|
|
|
2020-03-05 22:33:42 +00:00
|
|
|
// Only enforce known principals if disable custom sans is true.
|
|
|
|
var principals []string
|
|
|
|
if p.DisableCustomSANs {
|
|
|
|
principals = []string{
|
2019-07-30 00:54:38 +00:00
|
|
|
doc.PrivateIP,
|
|
|
|
fmt.Sprintf("ip-%s.%s.compute.internal", strings.Replace(doc.PrivateIP, ".", "-", -1), doc.Region),
|
2020-03-05 22:33:42 +00:00
|
|
|
}
|
2019-08-01 00:03:33 +00:00
|
|
|
}
|
2020-03-05 22:33:42 +00:00
|
|
|
|
|
|
|
// Default to cert type to host
|
|
|
|
defaults := SSHOptions{
|
|
|
|
CertType: SSHHostCert,
|
|
|
|
Principals: principals,
|
|
|
|
}
|
|
|
|
|
2019-08-01 00:03:33 +00:00
|
|
|
// Validate user options
|
2020-01-24 06:04:34 +00:00
|
|
|
signOptions = append(signOptions, sshCertOptionsValidator(defaults))
|
2019-08-01 00:03:33 +00:00
|
|
|
// Set defaults if not given as user options
|
2019-12-20 21:30:05 +00:00
|
|
|
signOptions = append(signOptions, sshCertDefaultsModifier(defaults))
|
2019-07-30 00:54:38 +00:00
|
|
|
|
|
|
|
return append(signOptions,
|
2019-09-05 01:31:09 +00:00
|
|
|
// Set the default extensions.
|
2019-07-30 00:54:38 +00:00
|
|
|
&sshDefaultExtensionModifier{},
|
2019-09-05 01:31:09 +00:00
|
|
|
// Set the validity bounds if not set.
|
2020-01-04 02:22:02 +00:00
|
|
|
&sshDefaultDuration{p.claimer},
|
2019-09-05 01:31:09 +00:00
|
|
|
// Validate public key
|
2019-09-11 00:04:13 +00:00
|
|
|
&sshDefaultPublicKeyValidator{},
|
2019-09-05 01:31:09 +00:00
|
|
|
// Validate the validity period.
|
2020-01-24 06:04:34 +00:00
|
|
|
&sshCertValidityValidator{p.claimer},
|
2019-09-05 01:31:09 +00:00
|
|
|
// Require all the fields in the SSH certificate
|
2020-01-24 06:04:34 +00:00
|
|
|
&sshCertDefaultValidator{},
|
2019-07-30 00:54:38 +00:00
|
|
|
), nil
|
2019-07-29 22:54:07 +00:00
|
|
|
}
|