2019-04-18 00:28:21 +00:00
|
|
|
package provisioner
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2019-07-29 22:54:07 +00:00
|
|
|
"context"
|
2019-04-18 00:28:21 +00:00
|
|
|
"crypto/sha256"
|
|
|
|
"crypto/x509"
|
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
2021-11-12 23:46:34 +00:00
|
|
|
"io"
|
2019-04-18 00:28:21 +00:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2022-04-21 23:20:38 +00:00
|
|
|
|
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"
|
2022-09-30 00:16:26 +00:00
|
|
|
"go.step.sm/linkedca"
|
2022-04-21 23:20:38 +00:00
|
|
|
|
|
|
|
"github.com/smallstep/certificates/errs"
|
2023-07-24 05:29:22 +00:00
|
|
|
"github.com/smallstep/certificates/webhook"
|
2019-04-18 00:28:21 +00:00
|
|
|
)
|
|
|
|
|
2019-05-10 01:56:24 +00:00
|
|
|
// gcpCertsURL is the url that serves Google OAuth2 public keys.
|
2019-04-23 21:33:36 +00:00
|
|
|
const gcpCertsURL = "https://www.googleapis.com/oauth2/v3/certs"
|
2019-04-18 23:01:30 +00:00
|
|
|
|
|
|
|
// gcpIdentityURL is the base url for the identity document in GCP.
|
2019-04-23 21:33:36 +00:00
|
|
|
const gcpIdentityURL = "http://metadata/computeMetadata/v1/instance/service-accounts/default/identity"
|
2019-04-18 23:01:30 +00:00
|
|
|
|
2019-04-18 00:28:21 +00:00
|
|
|
// gcpPayload extends jwt.Claims with custom GCP attributes.
|
|
|
|
type gcpPayload struct {
|
|
|
|
jose.Claims
|
|
|
|
AuthorizedParty string `json:"azp"`
|
|
|
|
Email string `json:"email"`
|
|
|
|
EmailVerified bool `json:"email_verified"`
|
|
|
|
Google gcpGooglePayload `json:"google"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type gcpGooglePayload struct {
|
|
|
|
ComputeEngine gcpComputeEnginePayload `json:"compute_engine"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type gcpComputeEnginePayload struct {
|
|
|
|
InstanceID string `json:"instance_id"`
|
|
|
|
InstanceName string `json:"instance_name"`
|
|
|
|
InstanceCreationTimestamp *jose.NumericDate `json:"instance_creation_timestamp"`
|
|
|
|
ProjectID string `json:"project_id"`
|
|
|
|
ProjectNumber int64 `json:"project_number"`
|
|
|
|
Zone string `json:"zone"`
|
|
|
|
LicenseID []string `json:"license_id"`
|
|
|
|
}
|
|
|
|
|
2019-04-23 21:33:36 +00:00
|
|
|
type gcpConfig struct {
|
|
|
|
CertsURL string
|
|
|
|
IdentityURL string
|
|
|
|
}
|
|
|
|
|
|
|
|
func newGCPConfig() *gcpConfig {
|
|
|
|
return &gcpConfig{
|
|
|
|
CertsURL: gcpCertsURL,
|
|
|
|
IdentityURL: gcpIdentityURL,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-18 00:28:21 +00:00
|
|
|
// GCP is the provisioner that supports identity tokens created by the Google
|
|
|
|
// Cloud Platform metadata API.
|
2019-04-24 20:05:46 +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
|
|
|
//
|
|
|
|
// If InstanceAge is set, only the instances with an instance_creation_timestamp
|
|
|
|
// within the given period will be accepted.
|
|
|
|
//
|
|
|
|
// Google Identity docs are available at
|
|
|
|
// https://cloud.google.com/compute/docs/instances/verifying-instance-identity
|
2019-04-18 00:28:21 +00:00
|
|
|
type GCP 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"`
|
|
|
|
ServiceAccounts []string `json:"serviceAccounts"`
|
|
|
|
ProjectIDs []string `json:"projectIDs"`
|
|
|
|
DisableCustomSANs bool `json:"disableCustomSANs"`
|
|
|
|
DisableTrustOnFirstUse bool `json:"disableTrustOnFirstUse"`
|
|
|
|
InstanceAge Duration `json:"instanceAge,omitempty"`
|
|
|
|
Claims *Claims `json:"claims,omitempty"`
|
|
|
|
Options *Options `json:"options,omitempty"`
|
2019-04-24 20:05:46 +00:00
|
|
|
config *gcpConfig
|
|
|
|
keyStore *keyStore
|
2022-03-10 02:43:45 +00:00
|
|
|
ctl *Controller
|
2019-04-18 00:28:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetID returns the provisioner unique identifier. The name should uniquely
|
|
|
|
// identify any GCP provisioner.
|
|
|
|
func (p *GCP) 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 *GCP) GetIDForToken() string {
|
2019-06-04 00:19:44 +00:00
|
|
|
return "gcp/" + p.Name
|
2019-04-18 00:28:21 +00:00
|
|
|
}
|
|
|
|
|
2019-04-24 20:05:46 +00:00
|
|
|
// GetTokenID returns the identifier of the token. The default value for GCP the
|
|
|
|
// SHA256 of "provisioner_id.instance_id", but if DisableTrustOnFirstUse is set
|
|
|
|
// to true, then it will be the SHA256 of the token.
|
2019-04-18 00:28:21 +00:00
|
|
|
func (p *GCP) GetTokenID(token string) (string, error) {
|
|
|
|
jwt, err := jose.ParseSigned(token)
|
|
|
|
if err != nil {
|
|
|
|
return "", errors.Wrap(err, "error parsing token")
|
|
|
|
}
|
|
|
|
|
2019-04-24 20:05:46 +00:00
|
|
|
// If TOFU is disabled create an ID for the token, so it cannot be reused.
|
|
|
|
if p.DisableTrustOnFirstUse {
|
|
|
|
sum := sha256.Sum256([]byte(token))
|
|
|
|
return strings.ToLower(hex.EncodeToString(sum[:])), nil
|
|
|
|
}
|
|
|
|
|
2019-04-18 00:28:21 +00:00
|
|
|
// Get claims w/out verification.
|
|
|
|
var claims gcpPayload
|
|
|
|
if err = jwt.UnsafeClaimsWithoutVerification(&claims); err != nil {
|
|
|
|
return "", errors.Wrap(err, "error verifying claims")
|
|
|
|
}
|
2019-04-24 20:05:46 +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.
|
2021-05-03 19:48:20 +00:00
|
|
|
unique := fmt.Sprintf("%s.%s", p.GetIDForToken(), claims.Google.ComputeEngine.InstanceID)
|
2019-04-18 00:28:21 +00:00
|
|
|
sum := sha256.Sum256([]byte(unique))
|
|
|
|
return strings.ToLower(hex.EncodeToString(sum[:])), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetName returns the name of the provisioner.
|
|
|
|
func (p *GCP) GetName() string {
|
|
|
|
return p.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetType returns the type of provisioner.
|
|
|
|
func (p *GCP) GetType() Type {
|
|
|
|
return TypeGCP
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetEncryptedKey is not available in a GCP provisioner.
|
2021-10-08 18:59:57 +00:00
|
|
|
func (p *GCP) GetEncryptedKey() (kid, key string, ok bool) {
|
2019-04-18 00:28:21 +00:00
|
|
|
return "", "", false
|
|
|
|
}
|
|
|
|
|
2019-04-18 23:01:30 +00:00
|
|
|
// GetIdentityURL returns the url that generates the GCP token.
|
2019-06-04 00:19:44 +00:00
|
|
|
func (p *GCP) GetIdentityURL(audience string) string {
|
2019-04-23 21:33:36 +00:00
|
|
|
// Initialize config if required
|
|
|
|
p.assertConfig()
|
|
|
|
|
2019-04-18 23:01:30 +00:00
|
|
|
q := url.Values{}
|
2019-06-04 00:19:44 +00:00
|
|
|
q.Add("audience", audience)
|
2019-04-18 23:01:30 +00:00
|
|
|
q.Add("format", "full")
|
|
|
|
q.Add("licenses", "FALSE")
|
2019-04-23 21:33:36 +00:00
|
|
|
return fmt.Sprintf("%s?%s", p.config.IdentityURL, q.Encode())
|
2019-04-18 23:01:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetIdentityToken does an HTTP request to the identity url.
|
2019-07-15 22:52:36 +00:00
|
|
|
func (p *GCP) GetIdentityToken(subject, caURL string) (string, error) {
|
2023-05-10 06:47:28 +00:00
|
|
|
_ = subject // unused input
|
|
|
|
|
2021-05-03 19:48:20 +00:00
|
|
|
audience, err := generateSignAudience(caURL, p.GetIDForToken())
|
2019-06-04 00:19:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err := http.NewRequest("GET", p.GetIdentityURL(audience), http.NoBody)
|
2019-04-18 23:01:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", errors.Wrap(err, "error creating identity request")
|
|
|
|
}
|
|
|
|
req.Header.Set("Metadata-Flavor", "Google")
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return "", errors.Wrap(err, "error doing identity request, are you in a GCP VM?")
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
2021-11-12 23:46:34 +00:00
|
|
|
b, err := io.ReadAll(resp.Body)
|
2019-04-18 23:01:30 +00:00
|
|
|
if err != nil {
|
2019-04-19 17:44:11 +00:00
|
|
|
return "", errors.Wrap(err, "error on identity request")
|
2019-04-18 23:01:30 +00:00
|
|
|
}
|
|
|
|
if resp.StatusCode >= 400 {
|
2019-04-19 17:44:11 +00:00
|
|
|
return "", errors.Errorf("error on identity request: status=%d, response=%s", resp.StatusCode, b)
|
2019-04-18 23:01:30 +00:00
|
|
|
}
|
|
|
|
return string(bytes.TrimSpace(b)), nil
|
|
|
|
}
|
|
|
|
|
2019-04-23 21:20:14 +00:00
|
|
|
// Init validates and initializes the GCP provisioner.
|
2022-03-10 02:43:45 +00:00
|
|
|
func (p *GCP) Init(config Config) (err error) {
|
2019-04-18 00:28:21 +00:00
|
|
|
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 22:57:15 +00:00
|
|
|
case p.InstanceAge.Value() < 0:
|
|
|
|
return errors.New("provisioner instanceAge cannot be negative")
|
2019-04-18 00:28:21 +00:00
|
|
|
}
|
2022-03-10 02:43:45 +00:00
|
|
|
|
2019-04-23 21:33:36 +00:00
|
|
|
// Initialize config
|
|
|
|
p.assertConfig()
|
2022-03-10 02:43:45 +00:00
|
|
|
|
2019-04-18 00:38:24 +00:00
|
|
|
// Initialize key store
|
2022-03-10 02:43:45 +00:00
|
|
|
if p.keyStore, err = newKeyStore(p.config.CertsURL); err != nil {
|
|
|
|
return
|
2019-04-18 00:28:21 +00:00
|
|
|
}
|
2019-06-04 00:19:44 +00:00
|
|
|
|
2022-03-10 02:43:45 +00:00
|
|
|
config.Audiences = config.Audiences.WithFragment(p.GetIDForToken())
|
2022-04-21 23:20:38 +00:00
|
|
|
p.ctl, err = NewController(p, p.Claims, config, p.Options)
|
2022-03-10 02:43:45 +00:00
|
|
|
return
|
2019-04-18 00:28:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// AuthorizeSign validates the given token and returns the sign options that
|
|
|
|
// will be used on certificate creation.
|
2023-10-06 21:02:19 +00:00
|
|
|
func (p *GCP) AuthorizeSign(ctx context.Context, token string) ([]SignOption, error) {
|
2019-04-18 00:28:21 +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, "gcp.AuthorizeSign")
|
2019-04-18 00:28:21 +00:00
|
|
|
}
|
2019-04-24 20:05:46 +00:00
|
|
|
|
2019-07-29 22:54:07 +00:00
|
|
|
ce := claims.Google.ComputeEngine
|
2020-07-13 23:06:01 +00:00
|
|
|
|
|
|
|
// Template options
|
|
|
|
data := x509util.NewTemplateData()
|
2020-07-13 23:09:40 +00:00
|
|
|
data.SetCommonName(ce.InstanceName)
|
2020-07-21 18:41:36 +00:00
|
|
|
if v, err := unsafeParseSigned(token); err == nil {
|
|
|
|
data.SetToken(v)
|
|
|
|
}
|
2020-07-13 23:06:01 +00:00
|
|
|
|
2019-07-15 22:52:36 +00:00
|
|
|
// Enforce known common name and default DNS if configured.
|
|
|
|
// By default we 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.
|
|
|
|
var so []SignOption
|
|
|
|
if p.DisableCustomSANs {
|
2019-07-15 22:52:36 +00:00
|
|
|
dnsName1 := fmt.Sprintf("%s.c.%s.internal", ce.InstanceName, ce.ProjectID)
|
|
|
|
dnsName2 := fmt.Sprintf("%s.%s.c.%s.internal", ce.InstanceName, ce.Zone, ce.ProjectID)
|
2021-10-08 18:59:57 +00:00
|
|
|
so = append(so,
|
|
|
|
commonNameSliceValidator([]string{
|
|
|
|
ce.InstanceName, ce.InstanceID, dnsName1, dnsName2,
|
|
|
|
}),
|
|
|
|
dnsNamesValidator([]string{
|
|
|
|
dnsName1, dnsName2,
|
|
|
|
}),
|
|
|
|
ipAddressesValidator(nil),
|
|
|
|
emailAddressesValidator(nil),
|
2023-10-06 21:02:19 +00:00
|
|
|
newURIsValidator(ctx, nil),
|
2021-10-08 18:59:57 +00:00
|
|
|
)
|
2020-07-13 23:06:01 +00:00
|
|
|
|
|
|
|
// Template SANs
|
|
|
|
data.SetSANs([]string{dnsName1, dnsName2})
|
|
|
|
}
|
|
|
|
|
|
|
|
templateOptions, err := CustomTemplateOptions(p.Options, data, x509util.DefaultIIDLeafTemplate)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errs.Wrap(http.StatusInternalServerError, err, "gcp.AuthorizeSign")
|
2019-04-18 00:28:21 +00:00
|
|
|
}
|
|
|
|
|
2019-04-24 20:05:46 +00:00
|
|
|
return append(so,
|
2022-03-22 02:21:40 +00:00
|
|
|
p,
|
2020-07-13 23:06:01 +00:00
|
|
|
templateOptions,
|
2019-09-05 01:31:09 +00:00
|
|
|
// modifiers / withOptions
|
2023-07-20 17:59:38 +00:00
|
|
|
newProvisionerExtensionOption(TypeGCP, p.Name, claims.Subject, "InstanceID", ce.InstanceID, "InstanceName", ce.InstanceName).WithControllerOptions(p.ctl),
|
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()),
|
2022-05-05 10:32:53 +00:00
|
|
|
newX509NamePolicyValidator(p.ctl.getPolicy().getX509()),
|
2023-07-24 05:29:22 +00:00
|
|
|
p.ctl.newWebhookController(
|
|
|
|
data,
|
|
|
|
linkedca.Webhook_X509,
|
|
|
|
webhook.WithAuthorizationPrincipal(ce.InstanceID),
|
|
|
|
),
|
2019-04-24 20:05:46 +00:00
|
|
|
), nil
|
2019-04-18 00:28:21 +00:00
|
|
|
}
|
|
|
|
|
2019-12-20 21:30:05 +00:00
|
|
|
// AuthorizeRenew returns an error if the renewal is disabled.
|
|
|
|
func (p *GCP) AuthorizeRenew(ctx context.Context, cert *x509.Certificate) error {
|
2022-03-10 02:43:45 +00:00
|
|
|
return p.ctl.AuthorizeRenew(ctx, cert)
|
2019-04-18 00:28:21 +00:00
|
|
|
}
|
|
|
|
|
2019-04-23 21:33:36 +00:00
|
|
|
// assertConfig initializes the config if it has not been initialized.
|
|
|
|
func (p *GCP) assertConfig() {
|
|
|
|
if p.config == nil {
|
|
|
|
p.config = newGCPConfig()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-18 00:28:21 +00:00
|
|
|
// 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 *GCP) authorizeToken(token string) (*gcpPayload, error) {
|
|
|
|
jwt, err := jose.ParseSigned(token)
|
|
|
|
if err != nil {
|
2019-12-20 21:30:05 +00:00
|
|
|
return nil, errs.Wrap(http.StatusUnauthorized, err, "gcp.authorizeToken; error parsing gcp token")
|
2019-04-18 00:28:21 +00:00
|
|
|
}
|
|
|
|
if len(jwt.Headers) == 0 {
|
2020-01-24 06:04:34 +00:00
|
|
|
return nil, errs.Unauthorized("gcp.authorizeToken; error parsing gcp token - header is missing")
|
2019-04-18 00:28:21 +00:00
|
|
|
}
|
|
|
|
|
2019-04-18 00:38:24 +00:00
|
|
|
var found bool
|
2019-04-18 00:28:21 +00:00
|
|
|
var claims gcpPayload
|
2019-04-18 00:38:24 +00:00
|
|
|
kid := jwt.Headers[0].KeyID
|
|
|
|
keys := p.keyStore.Get(kid)
|
|
|
|
for _, key := range keys {
|
2019-04-18 23:01:30 +00:00
|
|
|
if err := jwt.Claims(key.Public(), &claims); err == nil {
|
2019-04-18 00:38:24 +00:00
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
2020-01-24 06:04:34 +00:00
|
|
|
return nil, errs.Unauthorized("gcp.authorizeToken; failed to validate gcp token payload - cannot find key for kid %s", kid)
|
2019-04-18 00:28:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// According to "rfc7519 JSON Web Token" acceptable skew should be no
|
|
|
|
// more than a few minutes.
|
2019-06-04 22:57:15 +00:00
|
|
|
now := time.Now().UTC()
|
2019-04-18 00:28:21 +00:00
|
|
|
if err = claims.ValidateWithLeeway(jose.Expected{
|
2019-06-04 00:19:44 +00:00
|
|
|
Issuer: "https://accounts.google.com",
|
2019-06-04 22:57:15 +00:00
|
|
|
Time: now,
|
2019-04-18 00:28:21 +00:00
|
|
|
}, time.Minute); err != nil {
|
2019-12-20 21:30:05 +00:00
|
|
|
return nil, errs.Wrap(http.StatusUnauthorized, err, "gcp.authorizeToken; invalid gcp token payload")
|
2019-04-18 00:28:21 +00:00
|
|
|
}
|
|
|
|
|
2019-06-04 00:19:44 +00:00
|
|
|
// validate audiences with the defaults
|
2022-03-10 02:43:45 +00:00
|
|
|
if !matchesAudience(claims.Audience, p.ctl.Audiences.Sign) {
|
2020-01-24 06:04:34 +00:00
|
|
|
return nil, errs.Unauthorized("gcp.authorizeToken; invalid gcp token - invalid audience claim (aud)")
|
2019-06-04 00:19:44 +00:00
|
|
|
}
|
|
|
|
|
2019-04-18 23:01:30 +00:00
|
|
|
// validate subject (service account)
|
2019-04-18 00:28:21 +00:00
|
|
|
if len(p.ServiceAccounts) > 0 {
|
|
|
|
var found bool
|
|
|
|
for _, sa := range p.ServiceAccounts {
|
2019-06-04 00:28:39 +00:00
|
|
|
if sa == claims.Subject || sa == claims.Email {
|
2019-04-18 00:28:21 +00:00
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
2020-01-24 06:04:34 +00:00
|
|
|
return nil, errs.Unauthorized("gcp.authorizeToken; invalid gcp token - invalid subject claim")
|
2019-04-18 00:28:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-03 18:56:42 +00:00
|
|
|
// validate projects
|
|
|
|
if len(p.ProjectIDs) > 0 {
|
|
|
|
var found bool
|
|
|
|
for _, pi := range p.ProjectIDs {
|
|
|
|
if pi == claims.Google.ComputeEngine.ProjectID {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
2020-01-24 06:04:34 +00:00
|
|
|
return nil, errs.Unauthorized("gcp.authorizeToken; invalid gcp token - invalid project id")
|
2019-06-03 18:56:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-04 22:57:15 +00:00
|
|
|
// validate instance age
|
|
|
|
if d := p.InstanceAge.Value(); d > 0 {
|
|
|
|
if now.Sub(claims.Google.ComputeEngine.InstanceCreationTimestamp.Time()) > d {
|
2020-01-24 06:04:34 +00:00
|
|
|
return nil, errs.Unauthorized("gcp.authorizeToken; token google.compute_engine.instance_creation_timestamp is too old")
|
2019-06-04 22:57:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-18 00:28:21 +00:00
|
|
|
switch {
|
|
|
|
case claims.Google.ComputeEngine.InstanceID == "":
|
2020-01-24 06:04:34 +00:00
|
|
|
return nil, errs.Unauthorized("gcp.authorizeToken; gcp token google.compute_engine.instance_id cannot be empty")
|
2019-04-18 23:01:30 +00:00
|
|
|
case claims.Google.ComputeEngine.InstanceName == "":
|
2020-01-24 06:04:34 +00:00
|
|
|
return nil, errs.Unauthorized("gcp.authorizeToken; gcp token google.compute_engine.instance_name cannot be empty")
|
2019-04-18 00:28:21 +00:00
|
|
|
case claims.Google.ComputeEngine.ProjectID == "":
|
2020-01-24 06:04:34 +00:00
|
|
|
return nil, errs.Unauthorized("gcp.authorizeToken; gcp token google.compute_engine.project_id cannot be empty")
|
2019-04-18 00:28:21 +00:00
|
|
|
case claims.Google.ComputeEngine.Zone == "":
|
2020-01-24 06:04:34 +00:00
|
|
|
return nil, errs.Unauthorized("gcp.authorizeToken; gcp token google.compute_engine.zone cannot be empty")
|
2019-04-18 00:28:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &claims, 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.
|
2023-05-10 06:47:28 +00:00
|
|
|
func (p *GCP) AuthorizeSSHSign(_ 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("gcp.AuthorizeSSHSign; sshCA is disabled for gcp provisioner '%s'", p.GetName())
|
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, "gcp.AuthorizeSSHSign")
|
2019-10-28 18:50:43 +00:00
|
|
|
}
|
|
|
|
|
2019-07-30 01:17:20 +00:00
|
|
|
ce := claims.Google.ComputeEngine
|
2020-08-03 22:11:42 +00:00
|
|
|
signOptions := []SignOption{}
|
2019-07-30 01:17:20 +00:00
|
|
|
|
2020-08-03 22:11:42 +00:00
|
|
|
// Enforce host certificate.
|
|
|
|
defaults := SignSSHOptions{
|
|
|
|
CertType: SSHHostCert,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validated principals.
|
2020-07-30 01:05:35 +00:00
|
|
|
principals := []string{
|
|
|
|
fmt.Sprintf("%s.c.%s.internal", ce.InstanceName, ce.ProjectID),
|
|
|
|
fmt.Sprintf("%s.%s.c.%s.internal", ce.InstanceName, ce.Zone, ce.ProjectID),
|
2019-07-30 01:17: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, ce.InstanceName, 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, "gcp.AuthorizeSSHSign")
|
|
|
|
}
|
|
|
|
signOptions = append(signOptions, templateOptions)
|
2019-07-30 01:17:20 +00:00
|
|
|
|
|
|
|
return append(signOptions,
|
2022-05-19 01:42:42 +00:00
|
|
|
p,
|
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{},
|
2022-01-03 11:25:24 +00:00
|
|
|
// Ensure that all principal names are allowed
|
2022-05-05 10:32:53 +00:00
|
|
|
newSSHNamePolicyValidator(p.ctl.getPolicy().getSSHHost(), nil),
|
2022-09-30 00:16:26 +00:00
|
|
|
// Call webhooks
|
2023-07-24 05:29:22 +00:00
|
|
|
p.ctl.newWebhookController(
|
|
|
|
data,
|
|
|
|
linkedca.Webhook_SSH,
|
|
|
|
webhook.WithAuthorizationPrincipal(ce.InstanceID),
|
|
|
|
),
|
2019-07-30 01:17:20 +00:00
|
|
|
), nil
|
2019-07-29 22:54:07 +00:00
|
|
|
}
|