2019-03-05 01:58:20 +00:00
|
|
|
package provisioner
|
|
|
|
|
|
|
|
import (
|
2019-07-29 22:54:07 +00:00
|
|
|
"context"
|
2019-03-06 22:54:56 +00:00
|
|
|
"crypto/x509"
|
2019-03-05 01:58:20 +00:00
|
|
|
"encoding/json"
|
2019-09-18 22:24:50 +00:00
|
|
|
"net"
|
2019-03-05 01:58:20 +00:00
|
|
|
"net/http"
|
2019-09-18 22:54:10 +00:00
|
|
|
"net/url"
|
|
|
|
"path"
|
2019-03-15 20:49:50 +00:00
|
|
|
"strings"
|
2019-03-05 01:58:20 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2022-03-24 11:36:12 +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-03-24 11:36:12 +00:00
|
|
|
|
|
|
|
"github.com/smallstep/certificates/errs"
|
2019-03-05 01:58:20 +00:00
|
|
|
)
|
|
|
|
|
2019-03-06 22:54:56 +00:00
|
|
|
// openIDConfiguration contains the necessary properties in the
|
|
|
|
// `/.well-known/openid-configuration` document.
|
2019-03-05 01:58:20 +00:00
|
|
|
type openIDConfiguration struct {
|
|
|
|
Issuer string `json:"issuer"`
|
|
|
|
JWKSetURI string `json:"jwks_uri"`
|
|
|
|
}
|
|
|
|
|
2019-03-11 19:48:46 +00:00
|
|
|
// Validate validates the values in a well-known OpenID configuration endpoint.
|
|
|
|
func (c openIDConfiguration) Validate() error {
|
|
|
|
switch {
|
|
|
|
case c.Issuer == "":
|
|
|
|
return errors.New("issuer cannot be empty")
|
|
|
|
case c.JWKSetURI == "":
|
|
|
|
return errors.New("jwks_uri cannot be empty")
|
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-05 01:58:20 +00:00
|
|
|
// openIDPayload represents the fields on the id_token JWT payload.
|
|
|
|
type openIDPayload struct {
|
|
|
|
jose.Claims
|
2021-05-05 01:29:47 +00:00
|
|
|
AtHash string `json:"at_hash"`
|
|
|
|
AuthorizedParty string `json:"azp"`
|
|
|
|
Email string `json:"email"`
|
|
|
|
EmailVerified bool `json:"email_verified"`
|
|
|
|
Hd string `json:"hd"`
|
|
|
|
Nonce string `json:"nonce"`
|
|
|
|
Groups []string `json:"groups"`
|
2019-03-05 01:58:20 +00:00
|
|
|
}
|
|
|
|
|
2021-09-23 22:49:28 +00:00
|
|
|
func (o *openIDPayload) IsAdmin(admins []string) bool {
|
|
|
|
if o.Email != "" {
|
|
|
|
email := sanitizeEmail(o.Email)
|
|
|
|
for _, e := range admins {
|
|
|
|
if email == sanitizeEmail(e) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// The groups and emails can be in the same array for now, but consider
|
|
|
|
// making a specialized option later.
|
|
|
|
for _, name := range o.Groups {
|
|
|
|
for _, admin := range admins {
|
|
|
|
if name == admin {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-03-05 01:58:20 +00:00
|
|
|
// OIDC represents an OAuth 2.0 OpenID Connect provider.
|
2019-03-15 01:00:11 +00:00
|
|
|
//
|
2019-03-15 18:10:52 +00:00
|
|
|
// ClientSecret is mandatory, but it can be an empty string.
|
2019-03-05 01:58:20 +00:00
|
|
|
type OIDC 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"`
|
|
|
|
ClientID string `json:"clientID"`
|
|
|
|
ClientSecret string `json:"clientSecret"`
|
|
|
|
ConfigurationEndpoint string `json:"configurationEndpoint"`
|
|
|
|
TenantID string `json:"tenantID,omitempty"`
|
|
|
|
Admins []string `json:"admins,omitempty"`
|
|
|
|
Domains []string `json:"domains,omitempty"`
|
|
|
|
Groups []string `json:"groups,omitempty"`
|
|
|
|
ListenAddress string `json:"listenAddress,omitempty"`
|
|
|
|
Claims *Claims `json:"claims,omitempty"`
|
|
|
|
Options *Options `json:"options,omitempty"`
|
2019-03-05 01:58:20 +00:00
|
|
|
configuration openIDConfiguration
|
|
|
|
keyStore *keyStore
|
2022-03-10 02:43:45 +00:00
|
|
|
ctl *Controller
|
2019-03-05 01:58:20 +00:00
|
|
|
}
|
|
|
|
|
2019-03-15 20:49:50 +00:00
|
|
|
func sanitizeEmail(email string) string {
|
|
|
|
if i := strings.LastIndex(email, "@"); i >= 0 {
|
|
|
|
email = email[:i] + strings.ToLower(email[i:])
|
|
|
|
}
|
|
|
|
return email
|
|
|
|
}
|
|
|
|
|
2019-03-06 22:54:56 +00:00
|
|
|
// GetID returns the provisioner unique identifier, the OIDC provisioner the
|
|
|
|
// uses the clientID for this.
|
|
|
|
func (o *OIDC) GetID() string {
|
2021-05-20 01:23:20 +00:00
|
|
|
if o.ID != "" {
|
|
|
|
return o.ID
|
|
|
|
}
|
|
|
|
return o.GetIDForToken()
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetIDForToken returns an identifier that will be used to load the provisioner
|
|
|
|
// from a token.
|
|
|
|
func (o *OIDC) GetIDForToken() string {
|
2019-03-06 22:54:56 +00:00
|
|
|
return o.ClientID
|
|
|
|
}
|
|
|
|
|
2019-03-05 08:07:13 +00:00
|
|
|
// GetTokenID returns the provisioner unique identifier, the OIDC provisioner the
|
|
|
|
// uses the clientID for this.
|
|
|
|
func (o *OIDC) GetTokenID(ott string) (string, error) {
|
|
|
|
// Validate payload
|
|
|
|
token, err := jose.ParseSigned(ott)
|
|
|
|
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 openIDPayload
|
|
|
|
if err = token.UnsafeClaimsWithoutVerification(&claims); err != nil {
|
|
|
|
return "", errors.Wrap(err, "error verifying claims")
|
|
|
|
}
|
|
|
|
return claims.Nonce, nil
|
|
|
|
}
|
|
|
|
|
2019-03-06 22:54:56 +00:00
|
|
|
// GetName returns the name of the provisioner.
|
|
|
|
func (o *OIDC) GetName() string {
|
|
|
|
return o.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetType returns the type of provisioner.
|
|
|
|
func (o *OIDC) GetType() Type {
|
|
|
|
return TypeOIDC
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetEncryptedKey is not available in an OIDC provisioner.
|
2021-10-08 18:59:57 +00:00
|
|
|
func (o *OIDC) GetEncryptedKey() (kid, key string, ok bool) {
|
2019-03-06 22:54:56 +00:00
|
|
|
return "", "", false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Init validates and initializes the OIDC provider.
|
2019-03-07 02:32:56 +00:00
|
|
|
func (o *OIDC) Init(config Config) (err error) {
|
2019-03-05 01:58:20 +00:00
|
|
|
switch {
|
2019-03-13 22:33:52 +00:00
|
|
|
case o.Type == "":
|
|
|
|
return errors.New("type cannot be empty")
|
2019-03-05 01:58:20 +00:00
|
|
|
case o.Name == "":
|
|
|
|
return errors.New("name cannot be empty")
|
|
|
|
case o.ClientID == "":
|
|
|
|
return errors.New("clientID cannot be empty")
|
|
|
|
case o.ConfigurationEndpoint == "":
|
|
|
|
return errors.New("configurationEndpoint cannot be empty")
|
|
|
|
}
|
|
|
|
|
2019-09-18 22:24:50 +00:00
|
|
|
// Validate listenAddress if given
|
|
|
|
if o.ListenAddress != "" {
|
|
|
|
if _, _, err := net.SplitHostPort(o.ListenAddress); err != nil {
|
|
|
|
return errors.Wrap(err, "error parsing listenAddress")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-11 19:48:46 +00:00
|
|
|
// Decode and validate openid-configuration endpoint
|
2019-09-18 22:54:10 +00:00
|
|
|
u, err := url.Parse(o.ConfigurationEndpoint)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "error parsing %s", o.ConfigurationEndpoint)
|
|
|
|
}
|
|
|
|
if !strings.Contains(u.Path, "/.well-known/openid-configuration") {
|
|
|
|
u.Path = path.Join(u.Path, "/.well-known/openid-configuration")
|
|
|
|
}
|
|
|
|
if err := getAndDecode(u.String(), &o.configuration); err != nil {
|
2019-03-05 01:58:20 +00:00
|
|
|
return err
|
|
|
|
}
|
2019-03-11 19:48:46 +00:00
|
|
|
if err := o.configuration.Validate(); err != nil {
|
|
|
|
return errors.Wrapf(err, "error parsing %s", o.ConfigurationEndpoint)
|
2019-03-05 01:58:20 +00:00
|
|
|
}
|
2020-04-24 21:36:32 +00:00
|
|
|
// Replace {tenantid} with the configured one
|
|
|
|
if o.TenantID != "" {
|
2021-10-08 18:59:57 +00:00
|
|
|
o.configuration.Issuer = strings.ReplaceAll(o.configuration.Issuer, "{tenantid}", o.TenantID)
|
2020-04-24 21:36:32 +00:00
|
|
|
}
|
2019-03-05 01:58:20 +00:00
|
|
|
// Get JWK key set
|
2019-03-06 22:54:56 +00:00
|
|
|
o.keyStore, err = newKeyStore(o.configuration.JWKSetURI)
|
2019-03-05 01:58:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-11-16 00:57:51 +00:00
|
|
|
|
2022-04-21 23:20:38 +00:00
|
|
|
o.ctl, err = NewController(o, o.Claims, config, o.Options)
|
2022-03-10 02:43:45 +00:00
|
|
|
return
|
2019-03-05 01:58:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ValidatePayload validates the given token payload.
|
|
|
|
func (o *OIDC) ValidatePayload(p openIDPayload) error {
|
|
|
|
// According to "rfc7519 JSON Web Token" acceptable skew should be no more
|
|
|
|
// than a few minutes.
|
|
|
|
if err := p.ValidateWithLeeway(jose.Expected{
|
|
|
|
Issuer: o.configuration.Issuer,
|
|
|
|
Audience: jose.Audience{o.ClientID},
|
2019-03-11 18:12:47 +00:00
|
|
|
Time: time.Now().UTC(),
|
2019-03-05 01:58:20 +00:00
|
|
|
}, time.Minute); err != nil {
|
2019-12-20 21:30:05 +00:00
|
|
|
return errs.Wrap(http.StatusUnauthorized, err, "validatePayload: failed to validate oidc token payload")
|
2019-03-05 01:58:20 +00:00
|
|
|
}
|
2019-03-15 20:49:50 +00:00
|
|
|
|
|
|
|
// Validate azp if present
|
2019-03-05 01:58:20 +00:00
|
|
|
if p.AuthorizedParty != "" && p.AuthorizedParty != o.ClientID {
|
2020-01-24 06:04:34 +00:00
|
|
|
return errs.Unauthorized("validatePayload: failed to validate oidc token payload: invalid azp")
|
2019-03-05 01:58:20 +00:00
|
|
|
}
|
2019-03-15 20:49:50 +00:00
|
|
|
|
|
|
|
// Validate domains (case-insensitive)
|
2021-09-23 22:49:28 +00:00
|
|
|
if p.Email != "" && len(o.Domains) > 0 && !p.IsAdmin(o.Admins) {
|
2019-03-15 20:49:50 +00:00
|
|
|
email := sanitizeEmail(p.Email)
|
|
|
|
var found bool
|
|
|
|
for _, d := range o.Domains {
|
|
|
|
if strings.HasSuffix(email, "@"+strings.ToLower(d)) {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
2023-02-23 12:24:09 +00:00
|
|
|
return errs.Unauthorized("validatePayload: failed to validate oidc token payload: email %q is not allowed", p.Email)
|
2019-03-15 20:49:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-25 08:50:55 +00:00
|
|
|
// Filter by oidc group claim
|
|
|
|
if len(o.Groups) > 0 {
|
|
|
|
var found bool
|
|
|
|
for _, group := range o.Groups {
|
|
|
|
for _, g := range p.Groups {
|
|
|
|
if g == group {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
2020-01-24 06:04:34 +00:00
|
|
|
return errs.Unauthorized("validatePayload: oidc token payload validation failed: invalid group")
|
2019-06-25 08:50:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-05 01:58:20 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-03-05 08:07:13 +00:00
|
|
|
// authorizeToken applies the most common provisioner authorization claims,
|
|
|
|
// leaving the rest to context specific methods.
|
|
|
|
func (o *OIDC) authorizeToken(token string) (*openIDPayload, error) {
|
2019-03-05 01:58:20 +00:00
|
|
|
jwt, err := jose.ParseSigned(token)
|
|
|
|
if err != nil {
|
2019-12-20 21:30:05 +00:00
|
|
|
return nil, errs.Wrap(http.StatusUnauthorized, err,
|
|
|
|
"oidc.AuthorizeToken; error parsing oidc token")
|
2019-03-05 01:58:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Parse claims to get the kid
|
2019-03-06 22:54:56 +00:00
|
|
|
var claims openIDPayload
|
2019-03-05 01:58:20 +00:00
|
|
|
if err := jwt.UnsafeClaimsWithoutVerification(&claims); err != nil {
|
2019-12-20 21:30:05 +00:00
|
|
|
return nil, errs.Wrap(http.StatusUnauthorized, err,
|
|
|
|
"oidc.AuthorizeToken; error parsing oidc token claims")
|
2019-03-05 01:58:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
found := false
|
|
|
|
kid := jwt.Headers[0].KeyID
|
|
|
|
keys := o.keyStore.Get(kid)
|
|
|
|
for _, key := range keys {
|
|
|
|
if err := jwt.Claims(key, &claims); err == nil {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
2020-01-24 06:04:34 +00:00
|
|
|
return nil, errs.Unauthorized("oidc.AuthorizeToken; cannot validate oidc token")
|
2019-03-05 01:58:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := o.ValidatePayload(claims); err != nil {
|
2019-12-20 21:30:05 +00:00
|
|
|
return nil, errs.Wrap(http.StatusInternalServerError, err, "oidc.AuthorizeToken")
|
2019-03-05 01:58:20 +00:00
|
|
|
}
|
|
|
|
|
2019-03-05 08:07:13 +00:00
|
|
|
return &claims, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// AuthorizeRevoke returns an error if the provisioner does not have rights to
|
|
|
|
// revoke the certificate with serial number in the `sub` property.
|
|
|
|
// Only tokens generated by an admin have the right to revoke a certificate.
|
2023-05-10 06:47:28 +00:00
|
|
|
func (o *OIDC) AuthorizeRevoke(_ context.Context, token string) error {
|
2019-03-05 08:07:13 +00:00
|
|
|
claims, err := o.authorizeToken(token)
|
|
|
|
if err != nil {
|
2019-12-20 21:30:05 +00:00
|
|
|
return errs.Wrap(http.StatusInternalServerError, err, "oidc.AuthorizeRevoke")
|
2019-03-05 08:07:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Only admins can revoke certificates.
|
2021-09-23 22:49:28 +00:00
|
|
|
if claims.IsAdmin(o.Admins) {
|
2019-03-05 08:07:13 +00:00
|
|
|
return nil
|
|
|
|
}
|
2021-09-23 22:49:28 +00:00
|
|
|
|
2020-01-24 06:04:34 +00:00
|
|
|
return errs.Unauthorized("oidc.AuthorizeRevoke; cannot revoke with non-admin oidc token")
|
2019-03-05 08:07:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// AuthorizeSign validates the given token.
|
2023-05-10 06:47:28 +00:00
|
|
|
func (o *OIDC) AuthorizeSign(_ context.Context, token string) ([]SignOption, error) {
|
2019-03-05 08:07:13 +00:00
|
|
|
claims, err := o.authorizeToken(token)
|
|
|
|
if err != nil {
|
2019-12-20 21:30:05 +00:00
|
|
|
return nil, errs.Wrap(http.StatusInternalServerError, err, "oidc.AuthorizeSign")
|
2019-03-05 08:07:13 +00:00
|
|
|
}
|
2019-07-29 22:54:07 +00:00
|
|
|
|
2020-07-09 22:22:00 +00:00
|
|
|
// Certificate templates
|
2020-07-15 01:30:04 +00:00
|
|
|
sans := []string{}
|
|
|
|
if claims.Email != "" {
|
|
|
|
sans = append(sans, claims.Email)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add uri SAN with iss#sub if issuer is a URL with schema.
|
|
|
|
//
|
|
|
|
// According to https://openid.net/specs/openid-connect-core-1_0.html the
|
|
|
|
// iss value is a case sensitive URL using the https scheme that contains
|
|
|
|
// scheme, host, and optionally, port number and path components and no
|
|
|
|
// query or fragment components.
|
|
|
|
if iss, err := url.Parse(claims.Issuer); err == nil && iss.Scheme != "" {
|
|
|
|
iss.Fragment = claims.Subject
|
|
|
|
sans = append(sans, iss.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
data := x509util.CreateTemplateData(claims.Subject, sans)
|
2020-07-21 18:41:36 +00:00
|
|
|
if v, err := unsafeParseSigned(token); err == nil {
|
|
|
|
data.SetToken(v)
|
|
|
|
}
|
2020-07-09 02:01:59 +00:00
|
|
|
|
2020-07-15 01:30:04 +00:00
|
|
|
// Use the default template unless no-templates are configured and email is
|
|
|
|
// an admin, in that case we will use the CR template.
|
|
|
|
defaultTemplate := x509util.DefaultLeafTemplate
|
2021-09-23 22:49:28 +00:00
|
|
|
if !o.Options.GetX509Options().HasTemplate() && claims.IsAdmin(o.Admins) {
|
2020-09-21 19:49:16 +00:00
|
|
|
defaultTemplate = x509util.DefaultAdminLeafTemplate
|
2020-07-15 01:30:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
templateOptions, err := CustomTemplateOptions(o.Options, data, defaultTemplate)
|
2020-07-09 02:01:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errs.Wrap(http.StatusInternalServerError, err, "oidc.AuthorizeSign")
|
|
|
|
}
|
|
|
|
|
2020-07-15 01:30:04 +00:00
|
|
|
return []SignOption{
|
2022-03-22 02:21:40 +00:00
|
|
|
o,
|
2020-07-09 02:01:59 +00:00
|
|
|
templateOptions,
|
2019-09-05 01:31:09 +00:00
|
|
|
// modifiers / withOptions
|
2023-07-20 17:59:38 +00:00
|
|
|
newProvisionerExtensionOption(TypeOIDC, o.Name, o.ClientID).WithControllerOptions(o.ctl),
|
2022-03-10 02:43:45 +00:00
|
|
|
profileDefaultDuration(o.ctl.Claimer.DefaultTLSCertDuration()),
|
2019-09-05 01:31:09 +00:00
|
|
|
// validators
|
|
|
|
defaultPublicKeyValidator{},
|
2022-03-10 02:43:45 +00:00
|
|
|
newValidityValidator(o.ctl.Claimer.MinTLSCertDuration(), o.ctl.Claimer.MaxTLSCertDuration()),
|
2022-05-05 10:32:53 +00:00
|
|
|
newX509NamePolicyValidator(o.ctl.getPolicy().getX509()),
|
2022-09-30 00:16:26 +00:00
|
|
|
// webhooks
|
|
|
|
o.ctl.newWebhookController(data, linkedca.Webhook_X509),
|
2020-07-15 01:30:04 +00:00
|
|
|
}, nil
|
2019-03-05 01:58:20 +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 (o *OIDC) AuthorizeRenew(ctx context.Context, cert *x509.Certificate) error {
|
2022-03-10 02:43:45 +00:00
|
|
|
return o.ctl.AuthorizeRenew(ctx, cert)
|
2019-03-06 22:54:56 +00:00
|
|
|
}
|
|
|
|
|
2019-10-28 18:50:43 +00:00
|
|
|
// AuthorizeSSHSign returns the list of SignOption for a SignSSH request.
|
|
|
|
func (o *OIDC) AuthorizeSSHSign(ctx context.Context, token string) ([]SignOption, error) {
|
2022-03-10 02:43:45 +00:00
|
|
|
if !o.ctl.Claimer.IsSSHCAEnabled() {
|
2021-05-03 19:48:20 +00:00
|
|
|
return nil, errs.Unauthorized("oidc.AuthorizeSSHSign; sshCA is disabled for oidc provisioner '%s'", o.GetName())
|
2019-10-28 18:50:43 +00:00
|
|
|
}
|
|
|
|
claims, err := o.authorizeToken(token)
|
|
|
|
if err != nil {
|
2019-12-20 21:30:05 +00:00
|
|
|
return nil, errs.Wrap(http.StatusInternalServerError, err, "oidc.AuthorizeSSHSign")
|
2019-10-28 18:50:43 +00:00
|
|
|
}
|
2019-07-29 22:54:07 +00:00
|
|
|
|
2022-09-05 04:43:32 +00:00
|
|
|
if claims.Subject == "" {
|
|
|
|
return nil, errs.Unauthorized("oidc.AuthorizeSSHSign: failed to validate oidc token payload: subject not found")
|
2019-08-01 00:03:33 +00:00
|
|
|
}
|
2019-07-29 22:54:07 +00:00
|
|
|
|
2022-09-05 04:43:32 +00:00
|
|
|
var data sshutil.TemplateData
|
|
|
|
if claims.Email == "" {
|
2022-12-15 01:51:50 +00:00
|
|
|
// If email is empty, use the Subject claim instead to create minimal
|
|
|
|
// data for the template to use.
|
2022-09-05 04:43:32 +00:00
|
|
|
data = sshutil.CreateTemplateData(sshutil.UserCert, claims.Subject, nil)
|
|
|
|
if v, err := unsafeParseSigned(token); err == nil {
|
|
|
|
data.SetToken(v)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Get the identity using either the default identityFunc or one injected
|
|
|
|
// externally. Note that the PreferredUsername might be empty.
|
|
|
|
// TBD: Would preferred_username present a safety issue here?
|
|
|
|
iden, err := o.ctl.GetIdentity(ctx, claims.Email)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errs.Wrap(http.StatusInternalServerError, err, "oidc.AuthorizeSSHSign")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Certificate templates.
|
|
|
|
data = sshutil.CreateTemplateData(sshutil.UserCert, claims.Email, iden.Usernames)
|
|
|
|
if v, err := unsafeParseSigned(token); err == nil {
|
|
|
|
data.SetToken(v)
|
|
|
|
}
|
|
|
|
// Add custom extensions added in the identity function.
|
|
|
|
for k, v := range iden.Permissions.Extensions {
|
|
|
|
data.AddExtension(k, v)
|
|
|
|
}
|
|
|
|
// Add custom critical options added in the identity function.
|
|
|
|
for k, v := range iden.Permissions.CriticalOptions {
|
|
|
|
data.AddCriticalOption(k, v)
|
|
|
|
}
|
2020-07-31 00:24:05 +00:00
|
|
|
}
|
2020-07-28 00:47:52 +00:00
|
|
|
|
2020-08-03 22:28:48 +00:00
|
|
|
// Use the default template unless no-templates are configured and email is
|
|
|
|
// an admin, in that case we will use the parameters in the request.
|
2021-09-23 22:49:28 +00:00
|
|
|
isAdmin := claims.IsAdmin(o.Admins)
|
2020-08-10 18:26:51 +00:00
|
|
|
defaultTemplate := sshutil.DefaultTemplate
|
2020-08-03 22:28:48 +00:00
|
|
|
if isAdmin && !o.Options.GetSSHOptions().HasTemplate() {
|
2020-08-10 18:26:51 +00:00
|
|
|
defaultTemplate = sshutil.DefaultAdminTemplate
|
2020-08-03 22:28:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
templateOptions, err := CustomSSHTemplateOptions(o.Options, data, defaultTemplate)
|
2020-07-28 00:47:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errs.Wrap(http.StatusInternalServerError, err, "jwk.AuthorizeSign")
|
|
|
|
}
|
|
|
|
signOptions := []SignOption{templateOptions}
|
|
|
|
|
2019-11-16 00:57:51 +00:00
|
|
|
// Admin users can use any principal, and can sign user and host certificates.
|
|
|
|
// Non-admin users can only use principals returned by the identityFunc, and
|
|
|
|
// can only sign user certificates.
|
2020-08-03 22:28:48 +00:00
|
|
|
if isAdmin {
|
|
|
|
signOptions = append(signOptions, &sshCertOptionsRequireValidator{
|
|
|
|
CertType: true,
|
|
|
|
KeyID: true,
|
|
|
|
Principals: true,
|
|
|
|
})
|
|
|
|
} else {
|
2020-07-29 00:58:25 +00:00
|
|
|
signOptions = append(signOptions, sshCertOptionsValidator(SignSSHOptions{
|
2022-12-15 01:51:50 +00:00
|
|
|
CertType: SSHUserCert,
|
2020-07-29 00:58:25 +00:00
|
|
|
}))
|
2019-08-01 00:03:33 +00:00
|
|
|
}
|
|
|
|
|
2019-07-29 22:54:07 +00:00
|
|
|
return append(signOptions,
|
2022-05-19 01:42:42 +00:00
|
|
|
o,
|
2019-09-05 01:31:09 +00:00
|
|
|
// Set the validity bounds if not set.
|
2022-03-10 02:43:45 +00:00
|
|
|
&sshDefaultDuration{o.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{o.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(o.ctl.getPolicy().getSSHHost(), o.ctl.getPolicy().getSSHUser()),
|
2022-09-30 00:16:26 +00:00
|
|
|
// Call webhooks
|
|
|
|
o.ctl.newWebhookController(data, linkedca.Webhook_SSH),
|
2019-07-29 22:54:07 +00:00
|
|
|
), nil
|
|
|
|
}
|
|
|
|
|
2019-10-28 18:50:43 +00:00
|
|
|
// AuthorizeSSHRevoke returns nil if the token is valid, false otherwise.
|
2023-05-10 06:47:28 +00:00
|
|
|
func (o *OIDC) AuthorizeSSHRevoke(_ context.Context, token string) error {
|
2019-10-28 18:50:43 +00:00
|
|
|
claims, err := o.authorizeToken(token)
|
|
|
|
if err != nil {
|
2019-12-20 21:30:05 +00:00
|
|
|
return errs.Wrap(http.StatusInternalServerError, err, "oidc.AuthorizeSSHRevoke")
|
2019-10-28 18:50:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Only admins can revoke certificates.
|
2021-09-23 22:49:28 +00:00
|
|
|
if claims.IsAdmin(o.Admins) {
|
|
|
|
return nil
|
2019-10-28 18:50:43 +00:00
|
|
|
}
|
2021-09-23 22:49:28 +00:00
|
|
|
|
|
|
|
return errs.Unauthorized("oidc.AuthorizeSSHRevoke; cannot revoke with non-admin oidc token")
|
2019-10-28 18:50:43 +00:00
|
|
|
}
|
|
|
|
|
2019-03-05 01:58:20 +00:00
|
|
|
func getAndDecode(uri string, v interface{}) error {
|
2022-08-23 19:43:48 +00:00
|
|
|
resp, err := http.Get(uri) //nolint:gosec // openid-configuration uri
|
2019-03-05 01:58:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "failed to connect to %s", uri)
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
if err := json.NewDecoder(resp.Body).Decode(v); err != nil {
|
|
|
|
return errors.Wrapf(err, "error reading %s", uri)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|