-
Notifications
You must be signed in to change notification settings - Fork 476
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use AWS SDK to retrieve ec2 instance identity doc #1360
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,27 +3,27 @@ package aws | |
import ( | ||
"context" | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
"strings" | ||
"sync" | ||
|
||
awsSdk "github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/aws/ec2metadata" | ||
"github.com/aws/aws-sdk-go/aws/session" | ||
"github.com/hashicorp/hcl" | ||
"github.com/spiffe/spire/pkg/agent/plugin/nodeattestor" | ||
"github.com/spiffe/spire/pkg/common/catalog" | ||
"github.com/spiffe/spire/pkg/common/plugin/aws" | ||
"github.com/spiffe/spire/proto/spire/common" | ||
spi "github.com/spiffe/spire/proto/spire/common/plugin" | ||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/status" | ||
|
||
"errors" | ||
|
||
spi "github.com/spiffe/spire/proto/spire/common/plugin" | ||
) | ||
|
||
const ( | ||
defaultIdentityDocumentURL = "http://169.254.169.254/latest/dynamic/instance-identity/document" | ||
defaultIdentitySignatureURL = "http://169.254.169.254/latest/dynamic/instance-identity/signature" | ||
docPath = "instance-identity/document" | ||
sigPath = "instance-identity/signature" | ||
) | ||
|
||
func BuiltIn() catalog.Plugin { | ||
|
@@ -36,15 +36,15 @@ func builtin(p *IIDAttestorPlugin) catalog.Plugin { | |
|
||
// IIDAttestorConfig configures a IIDAttestorPlugin. | ||
type IIDAttestorConfig struct { | ||
EC2MetadataEndpoint string `hcl:"ec2_metadata_endpoint"` | ||
IdentityDocumentURL string `hcl:"identity_document_url"` | ||
IdentitySignatureURL string `hcl:"identity_signature_url"` | ||
trustDomain string | ||
} | ||
|
||
// IIDAttestorPlugin implements aws nodeattestation in the agent. | ||
type IIDAttestorPlugin struct { | ||
config *IIDAttestorConfig | ||
mtx sync.RWMutex | ||
session *session.Session | ||
mtx sync.RWMutex | ||
} | ||
|
||
// New creates a new IIDAttestorPlugin. | ||
|
@@ -55,24 +55,25 @@ func New() *IIDAttestorPlugin { | |
// FetchAttestationData fetches attestation data from the aws metadata server and sends an attestation response | ||
// on given stream. | ||
func (p *IIDAttestorPlugin) FetchAttestationData(stream nodeattestor.NodeAttestor_FetchAttestationDataServer) error { | ||
c, err := p.getConfig() | ||
if err != nil { | ||
return err | ||
if p.session == nil { | ||
return errors.New("not configured") | ||
} | ||
|
||
docBytes, err := httpGetBytes(c.IdentityDocumentURL) | ||
client := ec2metadata.New(p.session) | ||
|
||
doc, err := client.GetDynamicData(docPath) | ||
if err != nil { | ||
return aws.AttestationStepError("retrieving the IID from AWS", err) | ||
} | ||
|
||
sigBytes, err := httpGetBytes(c.IdentitySignatureURL) | ||
sig, err := client.GetDynamicData(sigPath) | ||
if err != nil { | ||
return aws.AttestationStepError("retrieving the IID signature from AWS", err) | ||
} | ||
|
||
attestationData := aws.IIDAttestationData{ | ||
Document: string(docBytes), | ||
Signature: string(sigBytes), | ||
Document: doc, | ||
Signature: sig, | ||
} | ||
|
||
respData, err := json.Marshal(attestationData) | ||
|
@@ -96,27 +97,30 @@ func (p *IIDAttestorPlugin) Configure(ctx context.Context, req *spi.ConfigureReq | |
return nil, status.Errorf(codes.InvalidArgument, "unable to decode configuration: %v", err) | ||
} | ||
|
||
if req.GlobalConfig == nil { | ||
return nil, status.Error(codes.InvalidArgument, "global configuration is required") | ||
} | ||
if req.GlobalConfig.TrustDomain == "" { | ||
return nil, status.Error(codes.InvalidArgument, "global configuration missing trust domain") | ||
// If the endpoint isn't explicitly configured but the legacy URLs are, extract the endpoint from it | ||
// This code is transitional, here until these deprecated configs are removed | ||
if config.EC2MetadataEndpoint == "" && (config.IdentityDocumentURL != "" || config.IdentitySignatureURL != "") { | ||
endpoint, err := endpointFromLegacyConfig(config.IdentityDocumentURL, config.IdentitySignatureURL) | ||
if err != nil { | ||
return nil, status.Error(codes.InvalidArgument, err.Error()) | ||
} | ||
config.EC2MetadataEndpoint = endpoint | ||
} | ||
// Set local vars from config struct | ||
config.trustDomain = req.GlobalConfig.TrustDomain | ||
|
||
if config.IdentityDocumentURL == "" { | ||
config.IdentityDocumentURL = defaultIdentityDocumentURL | ||
awsCfg := awsSdk.NewConfig() | ||
|
||
if config.EC2MetadataEndpoint != "" { | ||
awsCfg.WithEndpoint(config.EC2MetadataEndpoint) | ||
} | ||
|
||
if config.IdentitySignatureURL == "" { | ||
config.IdentitySignatureURL = defaultIdentitySignatureURL | ||
newSession, err := session.NewSession(awsCfg) | ||
if err != nil { | ||
return nil, status.Error(codes.InvalidArgument, err.Error()) | ||
} | ||
|
||
p.mtx.Lock() | ||
defer p.mtx.Unlock() | ||
|
||
p.config = config | ||
p.session = newSession | ||
|
||
return &spi.ConfigureResponse{}, nil | ||
} | ||
|
@@ -126,30 +130,24 @@ func (*IIDAttestorPlugin) GetPluginInfo(context.Context, *spi.GetPluginInfoReque | |
return &spi.GetPluginInfoResponse{}, nil | ||
} | ||
|
||
func (p *IIDAttestorPlugin) getConfig() (*IIDAttestorConfig, error) { | ||
p.mtx.RLock() | ||
defer p.mtx.RUnlock() | ||
|
||
if p.config == nil { | ||
return nil, errors.New("not configured") | ||
// endpointFromLegacyConfig extracts the endpoint from legacy configuration values | ||
// This code is transitional, here until these deprecated configs are removed | ||
func endpointFromLegacyConfig(docURL, sigURL string) (string, error) { | ||
docSuffix := "/dynamic/" + docPath | ||
if !strings.HasSuffix(docURL, docSuffix) { | ||
return "", fmt.Errorf("IID URL '%s' doesn't end in expected suffix %s", docURL, docSuffix) | ||
} | ||
return p.config, nil | ||
} | ||
docEndpoint := strings.TrimSuffix(docURL, docSuffix) | ||
|
||
func httpGetBytes(url string) ([]byte, error) { | ||
resp, err := http.Get(url) //nolint: gosec // URL is provided via explicit configuration | ||
if err != nil { | ||
return nil, err | ||
sigSuffix := "/dynamic/" + sigPath | ||
if !strings.HasSuffix(sigURL, sigSuffix) { | ||
return "", fmt.Errorf("IID signature URL '%s' doesn't end in expected suffix %s", sigURL, sigSuffix) | ||
} | ||
defer resp.Body.Close() | ||
sigEndpoint := strings.TrimSuffix(sigURL, sigSuffix) | ||
|
||
if resp.StatusCode != http.StatusOK { | ||
return nil, fmt.Errorf("unexpected status code: %d", resp.StatusCode) | ||
if docEndpoint != sigEndpoint { | ||
return "", fmt.Errorf("IID URL and Signature URL had different endpoints: %s != %s", docEndpoint, sigEndpoint) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is source #2 of incompatibility: If you use two different endpoints it doesn't work -- the SDK only takes a single one. |
||
} | ||
|
||
bytes, err := ioutil.ReadAll(resp.Body) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return bytes, nil | ||
return docEndpoint, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is source #1 of incompatibility: If you don't use the regular path suffix, it's not going to work.