2019-05-04 00:30:54 +00:00
|
|
|
package provisioner
|
|
|
|
|
|
|
|
import (
|
2019-07-29 22:54:07 +00:00
|
|
|
"context"
|
2019-05-08 00:07:04 +00:00
|
|
|
"crypto/sha256"
|
2019-05-04 00:30:54 +00:00
|
|
|
"crypto/x509"
|
2019-05-08 00:07:04 +00:00
|
|
|
"encoding/hex"
|
|
|
|
"encoding/json"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2019-05-04 00:30:54 +00:00
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2019-05-08 00:07:04 +00:00
|
|
|
"github.com/smallstep/cli/jose"
|
2019-05-04 00:30:54 +00:00
|
|
|
)
|
|
|
|
|
2019-05-08 02:07:49 +00:00
|
|
|
// azureOIDCBaseURL is the base discovery url for Microsoft Azure tokens.
|
|
|
|
const azureOIDCBaseURL = "https://login.microsoftonline.com"
|
2019-05-08 00:07:04 +00:00
|
|
|
|
|
|
|
// azureIdentityTokenURL is the URL to get the identity token for an instance.
|
|
|
|
const azureIdentityTokenURL = "http://169.254.169.254/metadata/identity/oauth2/token?api-version=2018-02-01&resource=https%3A%2F%2Fmanagement.azure.com%2F"
|
|
|
|
|
|
|
|
// azureDefaultAudience is the default audience used.
|
|
|
|
const azureDefaultAudience = "https://management.azure.com/"
|
|
|
|
|
|
|
|
// azureXMSMirIDRegExp is the regular expression used to parse the xms_mirid claim.
|
|
|
|
// Using case insensitive as resourceGroups appears as resourcegroups.
|
|
|
|
var azureXMSMirIDRegExp = regexp.MustCompile(`(?i)^/subscriptions/([^/]+)/resourceGroups/([^/]+)/providers/Microsoft.Compute/virtualMachines/([^/]+)$`)
|
2019-05-04 00:30:54 +00:00
|
|
|
|
|
|
|
type azureConfig struct {
|
2019-05-08 00:07:04 +00:00
|
|
|
oidcDiscoveryURL string
|
|
|
|
identityTokenURL string
|
2019-05-04 00:30:54 +00:00
|
|
|
}
|
|
|
|
|
2019-05-08 02:07:49 +00:00
|
|
|
func newAzureConfig(tenantID string) *azureConfig {
|
2019-05-04 00:30:54 +00:00
|
|
|
return &azureConfig{
|
2019-05-08 02:07:49 +00:00
|
|
|
oidcDiscoveryURL: azureOIDCBaseURL + "/" + tenantID + "/.well-known/openid-configuration",
|
2019-05-08 00:07:04 +00:00
|
|
|
identityTokenURL: azureIdentityTokenURL,
|
2019-05-04 00:30:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-08 00:07:04 +00:00
|
|
|
type azureIdentityToken struct {
|
|
|
|
AccessToken string `json:"access_token"`
|
|
|
|
RefreshToken string `json:"refresh_token"`
|
|
|
|
ClientID string `json:"client_id"`
|
|
|
|
ExpiresIn int64 `json:"expires_in,string"`
|
|
|
|
ExpiresOn int64 `json:"expires_on,string"`
|
|
|
|
ExtExpiresIn int64 `json:"ext_expires_in,string"`
|
|
|
|
NotBefore int64 `json:"not_before,string"`
|
|
|
|
Resource string `json:"resource"`
|
|
|
|
TokenType string `json:"token_type"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type azurePayload struct {
|
|
|
|
jose.Claims
|
|
|
|
AppID string `json:"appid"`
|
|
|
|
AppIDAcr string `json:"appidacr"`
|
|
|
|
IdentityProvider string `json:"idp"`
|
|
|
|
ObjectID string `json:"oid"`
|
|
|
|
TenantID string `json:"tid"`
|
|
|
|
Version string `json:"ver"`
|
|
|
|
XMSMirID string `json:"xms_mirid"`
|
|
|
|
}
|
|
|
|
|
2019-05-04 00:30:54 +00:00
|
|
|
// Azure is the provisioner that supports identity tokens created from the
|
|
|
|
// Microsoft Azure Instance Metadata service.
|
|
|
|
//
|
2019-05-08 00:07:04 +00:00
|
|
|
// The default audience is "https://management.azure.com/".
|
|
|
|
//
|
2019-05-04 00:30:54 +00:00
|
|
|
// 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
|
|
|
//
|
|
|
|
// Microsoft Azure identity docs are available at
|
|
|
|
// https://docs.microsoft.com/en-us/azure/active-directory/managed-identities-azure-resources/how-to-use-vm-token
|
|
|
|
// and https://docs.microsoft.com/en-us/azure/virtual-machines/windows/instance-metadata-service
|
2019-05-04 00:30:54 +00:00
|
|
|
type Azure struct {
|
2019-05-08 00:07:04 +00:00
|
|
|
Type string `json:"type"`
|
|
|
|
Name string `json:"name"`
|
2019-05-08 02:07:49 +00:00
|
|
|
TenantID string `json:"tenantId"`
|
2019-05-09 00:11:55 +00:00
|
|
|
ResourceGroups []string `json:"resourceGroups"`
|
2019-05-08 00:07:04 +00:00
|
|
|
Audience string `json:"audience,omitempty"`
|
|
|
|
DisableCustomSANs bool `json:"disableCustomSANs"`
|
|
|
|
DisableTrustOnFirstUse bool `json:"disableTrustOnFirstUse"`
|
|
|
|
Claims *Claims `json:"claims,omitempty"`
|
2019-05-04 00:30:54 +00:00
|
|
|
claimer *Claimer
|
|
|
|
config *azureConfig
|
2019-05-08 00:07:04 +00:00
|
|
|
oidcConfig openIDConfiguration
|
|
|
|
keyStore *keyStore
|
2019-05-04 00:30:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetID returns the provisioner unique identifier.
|
|
|
|
func (p *Azure) GetID() string {
|
2019-05-08 02:07:49 +00:00
|
|
|
return p.TenantID
|
2019-05-04 00:30:54 +00:00
|
|
|
}
|
|
|
|
|
2019-05-08 00:07:04 +00:00
|
|
|
// GetTokenID returns the identifier of the token. The default value for Azure
|
|
|
|
// the SHA256 of "xms_mirid", but if DisableTrustOnFirstUse is set to true, then
|
|
|
|
// it will be the token kid.
|
2019-05-04 00:30:54 +00:00
|
|
|
func (p *Azure) GetTokenID(token string) (string, error) {
|
2019-05-08 00:07:04 +00:00
|
|
|
jwt, err := jose.ParseSigned(token)
|
|
|
|
if err != nil {
|
|
|
|
return "", errors.Wrap(err, "error parsing token")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get claims w/out verification. We need to look up the provisioner
|
|
|
|
// key in order to verify the claims and we need the issuer from the claims
|
|
|
|
// before we can look up the provisioner.
|
|
|
|
var claims azurePayload
|
|
|
|
if err = jwt.UnsafeClaimsWithoutVerification(&claims); err != nil {
|
|
|
|
return "", errors.Wrap(err, "error verifying claims")
|
|
|
|
}
|
|
|
|
|
|
|
|
// If TOFU is disabled create return the token kid
|
|
|
|
if p.DisableTrustOnFirstUse {
|
|
|
|
return claims.ID, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
sum := sha256.Sum256([]byte(claims.XMSMirID))
|
|
|
|
return strings.ToLower(hex.EncodeToString(sum[:])), nil
|
2019-05-04 00:30:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetName returns the name of the provisioner.
|
|
|
|
func (p *Azure) GetName() string {
|
|
|
|
return p.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetType returns the type of provisioner.
|
|
|
|
func (p *Azure) GetType() Type {
|
|
|
|
return TypeAzure
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetEncryptedKey is not available in an Azure provisioner.
|
|
|
|
func (p *Azure) GetEncryptedKey() (kid string, key string, ok bool) {
|
|
|
|
return "", "", false
|
|
|
|
}
|
|
|
|
|
2019-05-10 01:56:24 +00:00
|
|
|
// GetIdentityToken retrieves from the metadata service the identity token and
|
|
|
|
// returns it.
|
2019-07-15 22:52:36 +00:00
|
|
|
func (p *Azure) GetIdentityToken(subject, caURL string) (string, error) {
|
2019-05-08 00:07:04 +00:00
|
|
|
// Initialize the config if this method is used from the cli.
|
2019-05-08 19:47:45 +00:00
|
|
|
p.assertConfig()
|
2019-05-08 00:07:04 +00:00
|
|
|
|
|
|
|
req, err := http.NewRequest("GET", p.config.identityTokenURL, http.NoBody)
|
|
|
|
if err != nil {
|
|
|
|
return "", errors.Wrap(err, "error creating request")
|
|
|
|
}
|
|
|
|
req.Header.Set("Metadata", "true")
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return "", errors.Wrap(err, "error getting identity token, are you in a Azure VM?")
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
b, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return "", errors.Wrap(err, "error reading identity token response")
|
|
|
|
}
|
|
|
|
if resp.StatusCode >= 400 {
|
|
|
|
return "", errors.Errorf("error getting identity token: status=%d, response=%s", resp.StatusCode, b)
|
|
|
|
}
|
|
|
|
|
|
|
|
var identityToken azureIdentityToken
|
|
|
|
if err := json.Unmarshal(b, &identityToken); err != nil {
|
|
|
|
return "", errors.Wrap(err, "error unmarshaling identity token response")
|
|
|
|
}
|
|
|
|
|
|
|
|
return identityToken.AccessToken, nil
|
|
|
|
}
|
|
|
|
|
2019-05-04 00:30:54 +00:00
|
|
|
// Init validates and initializes the Azure provisioner.
|
|
|
|
func (p *Azure) 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-05-08 02:07:49 +00:00
|
|
|
case p.TenantID == "":
|
|
|
|
return errors.New("provisioner tenantId cannot be empty")
|
2019-05-08 00:07:04 +00:00
|
|
|
case p.Audience == "": // use default audience
|
|
|
|
p.Audience = azureDefaultAudience
|
2019-05-04 00:30:54 +00:00
|
|
|
}
|
2019-05-08 02:07:49 +00:00
|
|
|
// Initialize config
|
2019-05-08 19:47:45 +00:00
|
|
|
p.assertConfig()
|
2019-05-08 00:07:04 +00:00
|
|
|
|
2019-05-04 00:30:54 +00:00
|
|
|
// Update claims with global ones
|
|
|
|
if p.claimer, err = NewClaimer(p.Claims, config.Claims); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-05-08 00:07:04 +00:00
|
|
|
|
|
|
|
// Decode and validate openid-configuration endpoint
|
|
|
|
if err := getAndDecode(p.config.oidcDiscoveryURL, &p.oidcConfig); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := p.oidcConfig.Validate(); err != nil {
|
|
|
|
return errors.Wrapf(err, "error parsing %s", p.config.oidcDiscoveryURL)
|
|
|
|
}
|
|
|
|
// Get JWK key set
|
|
|
|
if p.keyStore, err = newKeyStore(p.oidcConfig.JWKSetURI); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-05-04 00:30:54 +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 *Azure) AuthorizeSign(ctx context.Context, token string) ([]SignOption, error) {
|
2019-05-08 00:07:04 +00:00
|
|
|
jwt, err := jose.ParseSigned(token)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "error parsing token")
|
|
|
|
}
|
2019-05-08 02:07:49 +00:00
|
|
|
if len(jwt.Headers) == 0 {
|
|
|
|
return nil, errors.New("error parsing token: header is missing")
|
|
|
|
}
|
2019-05-08 00:07:04 +00:00
|
|
|
|
|
|
|
var found bool
|
|
|
|
var claims azurePayload
|
|
|
|
keys := p.keyStore.Get(jwt.Headers[0].KeyID)
|
|
|
|
for _, key := range keys {
|
2019-05-08 02:07:49 +00:00
|
|
|
if err := jwt.Claims(key.Public(), &claims); err == nil {
|
2019-05-08 00:07:04 +00:00
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
return nil, errors.New("cannot validate token")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := claims.ValidateWithLeeway(jose.Expected{
|
|
|
|
Audience: []string{p.Audience},
|
2019-05-08 02:07:49 +00:00
|
|
|
Issuer: p.oidcConfig.Issuer,
|
2019-05-08 00:07:04 +00:00
|
|
|
Time: time.Now(),
|
|
|
|
}, 1*time.Minute); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to validate payload")
|
|
|
|
}
|
|
|
|
|
2019-05-08 02:07:49 +00:00
|
|
|
// Validate TenantID
|
|
|
|
if claims.TenantID != p.TenantID {
|
|
|
|
return nil, errors.New("validation failed: invalid tenant id claim (tid)")
|
|
|
|
}
|
|
|
|
|
2019-05-08 00:07:04 +00:00
|
|
|
re := azureXMSMirIDRegExp.FindStringSubmatch(claims.XMSMirID)
|
2019-05-13 18:50:22 +00:00
|
|
|
if len(re) != 4 {
|
2019-05-08 00:07:04 +00:00
|
|
|
return nil, errors.Errorf("error parsing xms_mirid claim: %s", claims.XMSMirID)
|
|
|
|
}
|
2019-05-09 00:11:55 +00:00
|
|
|
group, name := re[2], re[3]
|
2019-05-08 00:07:04 +00:00
|
|
|
|
2019-05-09 00:11:55 +00:00
|
|
|
// Filter by resource group
|
|
|
|
if len(p.ResourceGroups) > 0 {
|
2019-05-08 00:07:04 +00:00
|
|
|
var found bool
|
2019-05-09 00:11:55 +00:00
|
|
|
for _, g := range p.ResourceGroups {
|
|
|
|
if g == group {
|
2019-05-08 00:07:04 +00:00
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
2019-05-09 00:11:55 +00:00
|
|
|
return nil, errors.New("validation failed: invalid resource group")
|
2019-05-08 00:07:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-29 22:54:07 +00:00
|
|
|
// Check for the sign ssh method, default to sign X.509
|
|
|
|
if m := MethodFromContext(ctx); m == SignSSHMethod {
|
2019-08-05 18:40:27 +00:00
|
|
|
if p.claimer.IsSSHCAEnabled() == false {
|
|
|
|
return nil, errors.Errorf("ssh ca is disabled for provisioner %s", p.GetID())
|
|
|
|
}
|
2019-07-30 01:01:20 +00:00
|
|
|
return p.authorizeSSHSign(claims, name)
|
2019-07-29 22:54:07 +00:00
|
|
|
}
|
|
|
|
|
2019-07-15 22:52:36 +00:00
|
|
|
// Enforce known common name and default DNS if configured.
|
|
|
|
// By default we'll accept the CN and SANs in the CSR.
|
2019-05-08 00:07:04 +00:00
|
|
|
// There's no way to trust them other than TOFU.
|
|
|
|
var so []SignOption
|
|
|
|
if p.DisableCustomSANs {
|
|
|
|
// name will work only inside the virtual network
|
2019-07-15 22:52:36 +00:00
|
|
|
so = append(so, commonNameValidator(name))
|
2019-05-08 00:07:04 +00:00
|
|
|
so = append(so, dnsNamesValidator([]string{name}))
|
|
|
|
}
|
|
|
|
|
|
|
|
return append(so,
|
2019-08-27 00:52:49 +00:00
|
|
|
defaultPublicKeyValidator{},
|
2019-05-08 00:07:04 +00:00
|
|
|
profileDefaultDuration(p.claimer.DefaultTLSCertDuration()),
|
2019-05-08 22:58:15 +00:00
|
|
|
newProvisionerExtensionOption(TypeAzure, p.Name, p.TenantID),
|
2019-05-08 00:07:04 +00:00
|
|
|
newValidityValidator(p.claimer.MinTLSCertDuration(), p.claimer.MaxTLSCertDuration()),
|
|
|
|
), nil
|
2019-05-04 00:30:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// AuthorizeRenewal returns an error if the renewal is disabled.
|
|
|
|
func (p *Azure) AuthorizeRenewal(cert *x509.Certificate) error {
|
|
|
|
if p.claimer.IsDisableRenewal() {
|
|
|
|
return errors.Errorf("renew is disabled for provisioner %s", p.GetID())
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// AuthorizeRevoke returns an error because revoke is not supported on Azure
|
|
|
|
// provisioners.
|
|
|
|
func (p *Azure) AuthorizeRevoke(token string) error {
|
|
|
|
return errors.New("revoke is not supported on a Azure provisioner")
|
|
|
|
}
|
2019-05-08 00:07:04 +00:00
|
|
|
|
2019-07-29 22:54:07 +00:00
|
|
|
// authorizeSSHSign returns the list of SignOption for a SignSSH request.
|
2019-07-30 01:01:20 +00:00
|
|
|
func (p *Azure) authorizeSSHSign(claims azurePayload, name string) ([]SignOption, error) {
|
|
|
|
signOptions := []SignOption{
|
|
|
|
// set the key id to the token subject
|
2019-07-30 01:04:01 +00:00
|
|
|
sshCertificateKeyIDModifier(name),
|
2019-07-30 01:01:20 +00:00
|
|
|
}
|
|
|
|
|
2019-08-01 00:03:33 +00:00
|
|
|
// Default to host + known hostnames
|
|
|
|
defaults := SSHOptions{
|
2019-07-30 01:01:20 +00:00
|
|
|
CertType: SSHHostCert,
|
|
|
|
Principals: []string{name},
|
2019-08-01 00:03:33 +00:00
|
|
|
}
|
|
|
|
// Validate user options
|
|
|
|
signOptions = append(signOptions, sshCertificateOptionsValidator(defaults))
|
|
|
|
// Set defaults if not given as user options
|
|
|
|
signOptions = append(signOptions, sshCertificateDefaultsModifier(defaults))
|
2019-07-30 01:01:20 +00:00
|
|
|
|
|
|
|
return append(signOptions,
|
|
|
|
// set the default extensions
|
|
|
|
&sshDefaultExtensionModifier{},
|
|
|
|
// checks the validity bounds, and set the validity if has not been set
|
|
|
|
&sshCertificateValidityModifier{p.claimer},
|
2019-09-11 00:04:13 +00:00
|
|
|
// validate public key
|
|
|
|
&sshDefaultPublicKeyValidator{},
|
2019-07-30 01:01:20 +00:00
|
|
|
// require all the fields in the SSH certificate
|
|
|
|
&sshCertificateDefaultValidator{},
|
|
|
|
), nil
|
2019-07-29 22:54:07 +00:00
|
|
|
}
|
|
|
|
|
2019-05-08 00:07:04 +00:00
|
|
|
// assertConfig initializes the config if it has not been initialized
|
2019-05-08 19:47:45 +00:00
|
|
|
func (p *Azure) assertConfig() {
|
|
|
|
if p.config == nil {
|
|
|
|
p.config = newAzureConfig(p.TenantID)
|
2019-05-08 00:07:04 +00:00
|
|
|
}
|
|
|
|
}
|