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"
|
2021-11-12 23:46:34 +00:00
|
|
|
"io"
|
2019-05-08 00:07:04 +00:00
|
|
|
"net/http"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2019-05-04 00:30:54 +00:00
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2019-12-20 21:30:05 +00:00
|
|
|
"github.com/smallstep/certificates/errs"
|
2020-08-24 21:44:11 +00:00
|
|
|
"go.step.sm/crypto/jose"
|
2020-08-10 18:26:51 +00:00
|
|
|
"go.step.sm/crypto/sshutil"
|
2020-08-05 23:02:46 +00:00
|
|
|
"go.step.sm/crypto/x509util"
|
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-10-28 18:50:43 +00:00
|
|
|
*base
|
2021-05-20 01:23:20 +00:00
|
|
|
ID string `json:"-"`
|
2020-07-31 01:44:52 +00:00
|
|
|
Type string `json:"type"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
TenantID string `json:"tenantID"`
|
|
|
|
ResourceGroups []string `json:"resourceGroups"`
|
2022-02-22 17:20:18 +00:00
|
|
|
SubscriptionIDs []string `json:"subscriptionIDs"`
|
2022-02-28 22:36:37 +00:00
|
|
|
ObjectIDs []string `json:"objectIDs"`
|
2020-07-31 01:44:52 +00:00
|
|
|
Audience string `json:"audience,omitempty"`
|
|
|
|
DisableCustomSANs bool `json:"disableCustomSANs"`
|
|
|
|
DisableTrustOnFirstUse bool `json:"disableTrustOnFirstUse"`
|
|
|
|
Claims *Claims `json:"claims,omitempty"`
|
|
|
|
Options *Options `json:"options,omitempty"`
|
2019-05-04 00:30:54 +00:00
|
|
|
config *azureConfig
|
2019-05-08 00:07:04 +00:00
|
|
|
oidcConfig openIDConfiguration
|
|
|
|
keyStore *keyStore
|
2022-03-10 02:43:45 +00:00
|
|
|
ctl *Controller
|
2019-05-04 00:30:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetID returns the provisioner unique identifier.
|
|
|
|
func (p *Azure) GetID() string {
|
2021-05-20 01:23:20 +00:00
|
|
|
if p.ID != "" {
|
|
|
|
return p.ID
|
|
|
|
}
|
|
|
|
return p.GetIDForToken()
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetIDForToken returns an identifier that will be used to load the provisioner
|
|
|
|
// from a token.
|
|
|
|
func (p *Azure) GetIDForToken() 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")
|
|
|
|
}
|
|
|
|
|
2021-08-11 18:50:54 +00:00
|
|
|
// If TOFU is disabled then allow token re-use. Azure caches the token for
|
|
|
|
// 24h and without allowing the re-use we cannot use it twice.
|
2019-05-08 00:07:04 +00:00
|
|
|
if p.DisableTrustOnFirstUse {
|
2021-08-11 18:50:54 +00:00
|
|
|
return "", ErrAllowTokenReuse
|
2019-05-08 00:07:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
2021-10-08 18:59:57 +00:00
|
|
|
func (p *Azure) GetEncryptedKey() (kid, key string, ok bool) {
|
2019-05-04 00:30:54 +00:00
|
|
|
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()
|
|
|
|
|
2021-11-12 23:46:34 +00:00
|
|
|
b, err := io.ReadAll(resp.Body)
|
2019-05-08 00:07:04 +00:00
|
|
|
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
|
|
|
}
|
2022-03-10 02:43:45 +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
|
|
|
|
|
|
|
// Decode and validate openid-configuration endpoint
|
2022-03-10 02:43:45 +00:00
|
|
|
if err = getAndDecode(p.config.oidcDiscoveryURL, &p.oidcConfig); err != nil {
|
|
|
|
return
|
2019-05-08 00:07:04 +00:00
|
|
|
}
|
|
|
|
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 {
|
2022-03-10 02:43:45 +00:00
|
|
|
return
|
2019-05-08 00:07:04 +00:00
|
|
|
}
|
|
|
|
|
2022-03-10 02:43:45 +00:00
|
|
|
p.ctl, err = NewController(p, p.Claims, config)
|
|
|
|
return
|
2019-05-04 00:30:54 +00:00
|
|
|
}
|
|
|
|
|
2022-02-22 17:20:18 +00:00
|
|
|
// authorizeToken returns the claims, name, group, subscription, identityObjectID, error.
|
|
|
|
func (p *Azure) authorizeToken(token string) (*azurePayload, string, string, string, string, error) {
|
2019-05-08 00:07:04 +00:00
|
|
|
jwt, err := jose.ParseSigned(token)
|
|
|
|
if err != nil {
|
2022-02-22 17:20:18 +00:00
|
|
|
return nil, "", "", "", "", errs.Wrap(http.StatusUnauthorized, err, "azure.authorizeToken; error parsing azure token")
|
2019-05-08 00:07:04 +00:00
|
|
|
}
|
2019-05-08 02:07:49 +00:00
|
|
|
if len(jwt.Headers) == 0 {
|
2022-02-22 17:20:18 +00:00
|
|
|
return nil, "", "", "", "", errs.Unauthorized("azure.authorizeToken; azure token missing header")
|
2019-05-08 02:07:49 +00:00
|
|
|
}
|
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 {
|
2022-02-22 17:20:18 +00:00
|
|
|
return nil, "", "", "", "", errs.Unauthorized("azure.authorizeToken; cannot validate azure token")
|
2019-05-08 00:07:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2022-02-22 17:20:18 +00:00
|
|
|
return nil, "", "", "", "", errs.Wrap(http.StatusUnauthorized, err, "azure.authorizeToken; failed to validate azure token payload")
|
2019-05-08 00:07:04 +00:00
|
|
|
}
|
|
|
|
|
2019-05-08 02:07:49 +00:00
|
|
|
// Validate TenantID
|
|
|
|
if claims.TenantID != p.TenantID {
|
2022-02-22 17:20:18 +00:00
|
|
|
return nil, "", "", "", "", errs.Unauthorized("azure.authorizeToken; azure token validation failed - invalid tenant id claim (tid)")
|
2019-05-08 02:07:49 +00:00
|
|
|
}
|
|
|
|
|
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 {
|
2022-02-22 17:20:18 +00:00
|
|
|
return nil, "", "", "", "", errs.Unauthorized("azure.authorizeToken; error parsing xms_mirid claim - %s", claims.XMSMirID)
|
2019-05-08 00:07:04 +00:00
|
|
|
}
|
2022-02-22 17:20:18 +00:00
|
|
|
identityObjectID := claims.ObjectID
|
|
|
|
subscription, group, name := re[1], re[2], re[3]
|
|
|
|
return &claims, name, group, subscription, identityObjectID, nil
|
2019-10-28 18:50:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// AuthorizeSign validates the given token and returns the sign options that
|
|
|
|
// will be used on certificate creation.
|
|
|
|
func (p *Azure) AuthorizeSign(ctx context.Context, token string) ([]SignOption, error) {
|
2022-02-22 17:20:18 +00:00
|
|
|
_, name, group, subscription, identityObjectID, err := p.authorizeToken(token)
|
2019-10-28 18:50:43 +00:00
|
|
|
if err != nil {
|
2019-12-20 21:30:05 +00:00
|
|
|
return nil, errs.Wrap(http.StatusInternalServerError, err, "azure.AuthorizeSign")
|
2019-10-28 18:50:43 +00:00
|
|
|
}
|
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 {
|
2020-01-24 06:04:34 +00:00
|
|
|
return nil, errs.Unauthorized("azure.AuthorizeSign; azure token validation failed - invalid resource group")
|
2019-05-08 00:07:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-22 17:20:18 +00:00
|
|
|
// Filter by subscription id
|
|
|
|
if len(p.SubscriptionIDs) > 0 {
|
|
|
|
var found bool
|
|
|
|
for _, s := range p.SubscriptionIDs {
|
|
|
|
if s == subscription {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
return nil, errs.Unauthorized("azure.AuthorizeSign; azure token validation failed - invalid subscription id")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filter by Azure AD identity object id
|
2022-02-24 09:26:45 +00:00
|
|
|
if len(p.ObjectIDs) > 0 {
|
2022-02-22 17:20:18 +00:00
|
|
|
var found bool
|
2022-02-24 09:26:45 +00:00
|
|
|
for _, i := range p.ObjectIDs {
|
2022-02-22 17:20:18 +00:00
|
|
|
if i == identityObjectID {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
return nil, errs.Unauthorized("azure.AuthorizeSign; azure token validation failed - invalid identity object id")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-13 22:20:52 +00:00
|
|
|
// Template options
|
|
|
|
data := x509util.NewTemplateData()
|
|
|
|
data.SetCommonName(name)
|
2020-07-21 18:41:36 +00:00
|
|
|
if v, err := unsafeParseSigned(token); err == nil {
|
|
|
|
data.SetToken(v)
|
|
|
|
}
|
2020-07-13 22:20:52 +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
|
2021-10-08 18:59:57 +00:00
|
|
|
so = append(so,
|
|
|
|
commonNameValidator(name),
|
|
|
|
dnsNamesValidator([]string{name}),
|
|
|
|
ipAddressesValidator(nil),
|
|
|
|
emailAddressesValidator(nil),
|
|
|
|
urisValidator(nil),
|
|
|
|
)
|
2020-07-13 22:20:52 +00:00
|
|
|
|
|
|
|
// Enforce SANs in the template.
|
|
|
|
data.SetSANs([]string{name})
|
|
|
|
}
|
|
|
|
|
|
|
|
templateOptions, err := CustomTemplateOptions(p.Options, data, x509util.DefaultIIDLeafTemplate)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errs.Wrap(http.StatusInternalServerError, err, "aws.AuthorizeSign")
|
2019-05-08 00:07:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return append(so,
|
2022-03-22 02:21:40 +00:00
|
|
|
p,
|
2020-07-13 22:20:52 +00:00
|
|
|
templateOptions,
|
2019-09-05 01:31:09 +00:00
|
|
|
// modifiers / withOptions
|
2019-05-08 22:58:15 +00:00
|
|
|
newProvisionerExtensionOption(TypeAzure, p.Name, p.TenantID),
|
2022-03-10 02:43:45 +00:00
|
|
|
profileDefaultDuration(p.ctl.Claimer.DefaultTLSCertDuration()),
|
2019-09-05 01:31:09 +00:00
|
|
|
// validators
|
|
|
|
defaultPublicKeyValidator{},
|
2022-03-10 02:43:45 +00:00
|
|
|
newValidityValidator(p.ctl.Claimer.MinTLSCertDuration(), p.ctl.Claimer.MaxTLSCertDuration()),
|
2019-05-08 00:07:04 +00:00
|
|
|
), nil
|
2019-05-04 00:30:54 +00:00
|
|
|
}
|
|
|
|
|
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 *Azure) AuthorizeRenew(ctx context.Context, cert *x509.Certificate) error {
|
2022-03-10 02:43:45 +00:00
|
|
|
return p.ctl.AuthorizeRenew(ctx, cert)
|
2019-05-04 00:30:54 +00:00
|
|
|
}
|
|
|
|
|
2019-10-28 18:50:43 +00:00
|
|
|
// AuthorizeSSHSign returns the list of SignOption for a SignSSH request.
|
|
|
|
func (p *Azure) AuthorizeSSHSign(ctx context.Context, token string) ([]SignOption, error) {
|
2022-03-10 02:43:45 +00:00
|
|
|
if !p.ctl.Claimer.IsSSHCAEnabled() {
|
2021-05-03 19:48:20 +00:00
|
|
|
return nil, errs.Unauthorized("azure.AuthorizeSSHSign; sshCA is disabled for provisioner '%s'", p.GetName())
|
2019-10-28 18:50:43 +00:00
|
|
|
}
|
2019-05-08 00:07:04 +00:00
|
|
|
|
2022-02-22 17:20:18 +00:00
|
|
|
_, name, _, _, _, err := p.authorizeToken(token)
|
2019-10-28 18:50:43 +00:00
|
|
|
if err != nil {
|
2019-12-20 21:30:05 +00:00
|
|
|
return nil, errs.Wrap(http.StatusInternalServerError, err, "azure.AuthorizeSSHSign")
|
2019-10-28 18:50:43 +00:00
|
|
|
}
|
2020-07-30 01:05:35 +00:00
|
|
|
|
2020-08-03 22:11:42 +00:00
|
|
|
signOptions := []SignOption{}
|
2020-07-30 01:05:35 +00:00
|
|
|
|
2020-08-03 22:11:42 +00:00
|
|
|
// Enforce host certificate.
|
2020-07-30 01:05:35 +00:00
|
|
|
defaults := SignSSHOptions{
|
|
|
|
CertType: SSHHostCert,
|
2019-07-30 01:01:20 +00:00
|
|
|
}
|
2020-08-03 22:11:42 +00:00
|
|
|
|
|
|
|
// Validated principals.
|
|
|
|
principals := []string{name}
|
2019-07-30 01:01:20 +00:00
|
|
|
|
2020-03-05 22:33:42 +00:00
|
|
|
// Only enforce known principals if disable custom sans is true.
|
|
|
|
if p.DisableCustomSANs {
|
2020-07-30 01:05:35 +00:00
|
|
|
defaults.Principals = principals
|
2020-08-03 22:11:42 +00:00
|
|
|
} else {
|
|
|
|
// Check that at least one principal is sent in the request.
|
|
|
|
signOptions = append(signOptions, &sshCertOptionsRequireValidator{
|
|
|
|
Principals: true,
|
|
|
|
})
|
2020-07-30 01:05:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Certificate templates.
|
|
|
|
data := sshutil.CreateTemplateData(sshutil.HostCert, name, principals)
|
|
|
|
if v, err := unsafeParseSigned(token); err == nil {
|
|
|
|
data.SetToken(v)
|
|
|
|
}
|
|
|
|
|
2020-08-10 18:26:51 +00:00
|
|
|
templateOptions, err := CustomSSHTemplateOptions(p.Options, data, sshutil.DefaultIIDTemplate)
|
2020-07-30 01:05:35 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errs.Wrap(http.StatusInternalServerError, err, "azure.AuthorizeSSHSign")
|
|
|
|
}
|
|
|
|
signOptions = append(signOptions, templateOptions)
|
2019-07-30 01:01:20 +00:00
|
|
|
|
|
|
|
return append(signOptions,
|
2020-08-03 22:11:42 +00:00
|
|
|
// Validate user SignSSHOptions.
|
|
|
|
sshCertOptionsValidator(defaults),
|
2019-09-05 01:31:09 +00:00
|
|
|
// Set the validity bounds if not set.
|
2022-03-10 02:43:45 +00:00
|
|
|
&sshDefaultDuration{p.ctl.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.
|
2022-03-10 02:43:45 +00:00
|
|
|
&sshCertValidityValidator{p.ctl.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 01:01:20 +00:00
|
|
|
), 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
|
|
|
}
|
|
|
|
}
|