Skip to content

Commit

Permalink
Fix EAS Token generation (#40154)
Browse files Browse the repository at this point in the history
In the latest release, we added support for S3 bucket as OIDC Provider
URL.
There are two places were the Token is generated.
One used in Discover Wizard, EICE access and Auto-Discover.
Another one for the External Audit Storage.

The first was updated to include the S3 Bucket as possible Issuer.
EAS flow was not updated.

This PR merges both flows.
  • Loading branch information
marcoandredinis authored Apr 5, 2024
1 parent a72d02d commit 2943df3
Show file tree
Hide file tree
Showing 5 changed files with 190 additions and 123 deletions.
46 changes: 7 additions & 39 deletions lib/auth/externalauditstorage.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,50 +24,18 @@ import (
"github.com/gravitational/trace"

"github.com/gravitational/teleport/api/types"
"github.com/gravitational/teleport/lib/integrations/externalauditstorage"
"github.com/gravitational/teleport/lib/jwt"
"github.com/gravitational/teleport/lib/services"
"github.com/gravitational/teleport/lib/integrations/awsoidc"
usagereporter "github.com/gravitational/teleport/lib/usagereporter/teleport"
"github.com/gravitational/teleport/lib/utils/oidc"
)

// GenerateExternalAuditStorageOIDCToken generates a signed OIDC token for use by
// the External Audit Storage feature when authenticating to customer AWS accounts.
func (a *Server) GenerateExternalAuditStorageOIDCToken(ctx context.Context) (string, error) {
clusterName, err := a.GetDomainName()
if err != nil {
return "", trace.Wrap(err)
}

ca, err := a.GetCertAuthority(ctx, types.CertAuthID{
Type: types.OIDCIdPCA,
DomainName: clusterName,
}, true /*loadKeys*/)
if err != nil {
return "", trace.Wrap(err)
}

signer, err := a.GetKeyStore().GetJWTSigner(ctx, ca)
if err != nil {
return "", trace.Wrap(err)
}

privateKey, err := services.GetJWTSigner(signer, ca.GetClusterName(), a.clock)
if err != nil {
return "", trace.Wrap(err)
}

issuer, err := oidc.IssuerForCluster(ctx, a)
if err != nil {
return "", trace.Wrap(err)
}

token, err := privateKey.SignAWSOIDC(jwt.SignParams{
Username: a.ServerID,
Audience: types.IntegrationAWSOIDCAudience,
Subject: types.IntegrationAWSOIDCSubjectAuth,
Issuer: issuer,
Expires: a.clock.Now().Add(externalauditstorage.TokenLifetime),
func (a *Server) GenerateExternalAuditStorageOIDCToken(ctx context.Context, integration string) (string, error) {
token, err := awsoidc.GenerateAWSOIDCToken(ctx, a, a.GetKeyStore(), awsoidc.GenerateAWSOIDCTokenRequest{
Integration: integration,
Username: a.ServerID,
Subject: types.IntegrationAWSOIDCSubjectAuth,
Clock: a.clock,
})
if err != nil {
return "", trace.Wrap(err)
Expand Down
86 changes: 9 additions & 77 deletions lib/auth/integration/integrationv1/awsoidc.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,6 @@ package integrationv1

import (
"context"
"fmt"
"net/url"
"strings"
"time"

"github.com/gravitational/trace"
"github.com/jonboulle/clockwork"
Expand All @@ -34,10 +30,8 @@ import (
"github.com/gravitational/teleport/api/types"
"github.com/gravitational/teleport/lib/authz"
"github.com/gravitational/teleport/lib/integrations/awsoidc"
"github.com/gravitational/teleport/lib/jwt"
"github.com/gravitational/teleport/lib/modules"
"github.com/gravitational/teleport/lib/services"
"github.com/gravitational/teleport/lib/utils/oidc"
)

// GenerateAWSOIDCToken generates a token to be used when executing an AWS OIDC Integration action.
Expand All @@ -51,83 +45,22 @@ func (s *Service) GenerateAWSOIDCToken(ctx context.Context, req *integrationpb.G
return nil, trace.Wrap(err)
}

var integration types.Integration
// Clients in v15 or lower might not send the Integration
// All v16+ clients will send the Integration
// TODO(marco) DELETE IN v17.0
if req.Integration != "" {
integration, err = s.cache.GetIntegration(ctx, req.Integration)
if err != nil {
return nil, trace.Wrap(err)
}
}

return s.generateAWSOIDCTokenWithoutAuthZ(ctx, integration)
return s.generateAWSOIDCTokenWithoutAuthZ(ctx, req.Integration)
}

// generateAWSOIDCTokenWithoutAuthZ generates a token to be used when executing an AWS OIDC Integration action.
// Bypasses authz and should only be used by other methods that validate AuthZ.
func (s *Service) generateAWSOIDCTokenWithoutAuthZ(ctx context.Context, integration types.Integration) (*integrationpb.GenerateAWSOIDCTokenResponse, error) {
func (s *Service) generateAWSOIDCTokenWithoutAuthZ(ctx context.Context, integrationName string) (*integrationpb.GenerateAWSOIDCTokenResponse, error) {
username, err := authz.GetClientUsername(ctx)
if err != nil {
return nil, trace.Wrap(err)
}

clusterName, err := s.cache.GetClusterName()
if err != nil {
return nil, trace.Wrap(err)
}

ca, err := s.cache.GetCertAuthority(ctx, types.CertAuthID{
Type: types.OIDCIdPCA,
DomainName: clusterName.GetClusterName(),
}, true)
if err != nil {
return nil, trace.Wrap(err)
}

// Extract the JWT signing key and sign the claims.
signer, err := s.keyStoreManager.GetJWTSigner(ctx, ca)
if err != nil {
return nil, trace.Wrap(err)
}

privateKey, err := services.GetJWTSigner(signer, ca.GetClusterName(), s.clock)
if err != nil {
return nil, trace.Wrap(err)
}

// Clients in v15 or lower might not send the Integration
// All v16+ clients will send the Integration
// TODO(marco) DELETE IN v17.0
// Checking for an empty issuer must be kept.
var issuer string
if integration != nil {
issuerS3URI := integration.GetAWSOIDCIntegrationSpec().IssuerS3URI
if issuerS3URI != "" {
issuerS3URL, err := url.Parse(issuerS3URI)
if err != nil {
return nil, trace.Wrap(err)
}
prefix := strings.TrimLeft(issuerS3URL.Path, "/")
issuer = fmt.Sprintf("https://%s.s3.amazonaws.com/%s", issuerS3URL.Host, prefix)
}
}
if issuer == "" {
issuer, err = oidc.IssuerForCluster(ctx, s.cache)
if err != nil {
return nil, trace.Wrap(err)
}
}

token, err := privateKey.SignAWSOIDC(jwt.SignParams{
Username: username,
Audience: types.IntegrationAWSOIDCAudience,
Subject: types.IntegrationAWSOIDCSubject,
Issuer: issuer,
// Token expiration is not controlled by the Expires property.
// It is defined by assumed IAM Role's "Maximum session duration" (usually 1h).
Expires: s.clock.Now().Add(time.Minute),
token, err := awsoidc.GenerateAWSOIDCToken(ctx, s.cache, s.keyStoreManager, awsoidc.GenerateAWSOIDCTokenRequest{
Integration: integrationName,
Username: username,
Subject: types.IntegrationAWSOIDCSubject,
Clock: s.clock,
})
if err != nil {
return nil, trace.Wrap(err)
Expand Down Expand Up @@ -232,12 +165,11 @@ func (s *AWSOIDCService) awsClientReq(ctx context.Context, integrationName, regi
return nil, trace.BadParameter("integration subkind (%s) mismatch", integration.GetSubKind())
}

awsoidcSpec := integration.GetAWSOIDCIntegrationSpec()
if awsoidcSpec == nil {
if integration.GetAWSOIDCIntegrationSpec() == nil {
return nil, trace.BadParameter("missing spec fields for %q (%q) integration", integration.GetName(), integration.GetSubKind())
}

token, err := s.integrationService.generateAWSOIDCTokenWithoutAuthZ(ctx, integration)
token, err := s.integrationService.generateAWSOIDCTokenWithoutAuthZ(ctx, integrationName)
if err != nil {
return nil, trace.Wrap(err)
}
Expand Down
165 changes: 165 additions & 0 deletions lib/integrations/awsoidc/token_generator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
/*
* Teleport
* Copyright (C) 2024 Gravitational, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package awsoidc

import (
"context"
"crypto"
"fmt"
"net/url"
"strings"
"time"

"github.com/gravitational/trace"
"github.com/jonboulle/clockwork"

"github.com/gravitational/teleport/api/types"
"github.com/gravitational/teleport/lib/jwt"
"github.com/gravitational/teleport/lib/services"
"github.com/gravitational/teleport/lib/utils/oidc"
)

// Cache is the subset of the cached resources that the AWS OIDC Token Generation queries.
type Cache interface {
// GetIntegration returns the specified integration resources.
GetIntegration(ctx context.Context, name string) (types.Integration, error)

// GetClusterName returns local cluster name of the current auth server
GetClusterName(...services.MarshalOption) (types.ClusterName, error)

// GetCertAuthority returns cert authority by id
GetCertAuthority(ctx context.Context, id types.CertAuthID, loadKeys bool) (types.CertAuthority, error)

// GetProxies returns a list of registered proxies.
GetProxies() ([]types.Server, error)
}

// KeyStoreManager defines methods to get signers using the server's keystore.
type KeyStoreManager interface {
// GetJWTSigner selects a usable JWT keypair from the given keySet and returns a [crypto.Signer].
GetJWTSigner(ctx context.Context, ca types.CertAuthority) (crypto.Signer, error)
}

// GenerateAWSOIDCTokenRequest contains the required elements to generate an AWS OIDC Token (JWT).
type GenerateAWSOIDCTokenRequest struct {
// Integration is the AWS OIDC Integration name.
Integration string
// Username is the JWT Username (on behalf of claim)
Username string
// Subject is the JWT Subject (subject claim)
Subject string
// Clock is used to mock time
Clock clockwork.Clock
}

// CheckAndSetDefaults checks the request params.
func (g *GenerateAWSOIDCTokenRequest) CheckAndSetDefaults() error {
if g.Username == "" {
return trace.BadParameter("username missing")
}
if g.Subject == "" {
return trace.BadParameter("missing subject")
}
if g.Clock == nil {
g.Clock = clockwork.NewRealClock()
}

return nil
}

// GenerateAWSOIDCToken generates a token to be used when executing an AWS OIDC Integration action.
func GenerateAWSOIDCToken(ctx context.Context, cacheClt Cache, keyStoreManager KeyStoreManager, req GenerateAWSOIDCTokenRequest) (string, error) {
var integration types.Integration
var err error
var issuer string

// Clients in v15 or lower might not send the Integration
// All v16+ clients will send the Integration
// For V17.0, if the Integration is empty, an error must be returned.
// TODO(marco) DELETE IN v17.0
if req.Integration != "" {
integration, err = cacheClt.GetIntegration(ctx, req.Integration)
if err != nil {
return "", trace.Wrap(err)
}

if integration.GetSubKind() != types.IntegrationSubKindAWSOIDC {
return "", trace.BadParameter("integration subkind (%s) mismatch", integration.GetSubKind())
}

if integration.GetAWSOIDCIntegrationSpec() == nil {
return "", trace.BadParameter("missing spec fields for %q (%q) integration", integration.GetName(), integration.GetSubKind())
}

issuerS3URI := integration.GetAWSOIDCIntegrationSpec().IssuerS3URI
if issuerS3URI != "" {
issuerS3URL, err := url.Parse(issuerS3URI)
if err != nil {
return "", trace.Wrap(err)
}
prefix := strings.TrimLeft(issuerS3URL.Path, "/")
issuer = fmt.Sprintf("https://%s.s3.amazonaws.com/%s", issuerS3URL.Host, prefix)
}
}

if issuer == "" {
issuer, err = oidc.IssuerForCluster(ctx, cacheClt)
if err != nil {
return "", trace.Wrap(err)
}
}

clusterName, err := cacheClt.GetClusterName()
if err != nil {
return "", trace.Wrap(err)
}

ca, err := cacheClt.GetCertAuthority(ctx, types.CertAuthID{
Type: types.OIDCIdPCA,
DomainName: clusterName.GetClusterName(),
}, true /*loadKeys*/)
if err != nil {
return "", trace.Wrap(err)
}

signer, err := keyStoreManager.GetJWTSigner(ctx, ca)
if err != nil {
return "", trace.Wrap(err)
}

privateKey, err := services.GetJWTSigner(signer, ca.GetClusterName(), req.Clock)
if err != nil {
return "", trace.Wrap(err)
}

token, err := privateKey.SignAWSOIDC(jwt.SignParams{
Username: req.Username,
Audience: types.IntegrationAWSOIDCAudience,
Subject: req.Subject,
Issuer: issuer,
// Token expiration is not controlled by the Expires property.
// It is defined by assumed IAM Role's "Maximum session duration" (usually 1h).
Expires: req.Clock.Now().Add(time.Hour),
})
if err != nil {
return "", trace.Wrap(err)
}

return token, nil
}
Loading

0 comments on commit 2943df3

Please sign in to comment.