1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
|
package imds
import (
"context"
"encoding/json"
"fmt"
"io"
"strings"
"time"
"github.com/aws/smithy-go"
smithyio "github.com/aws/smithy-go/io"
"github.com/aws/smithy-go/middleware"
smithyhttp "github.com/aws/smithy-go/transport/http"
)
const getIAMInfoPath = getMetadataPath + "/iam/info"
// GetIAMInfo retrieves an identity document describing an
// instance. Error is returned if the request fails or is unable to parse
// the response.
func (c *Client) GetIAMInfo(
ctx context.Context, params *GetIAMInfoInput, optFns ...func(*Options),
) (
*GetIAMInfoOutput, error,
) {
if params == nil {
params = &GetIAMInfoInput{}
}
result, metadata, err := c.invokeOperation(ctx, "GetIAMInfo", params, optFns,
addGetIAMInfoMiddleware,
)
if err != nil {
return nil, err
}
out := result.(*GetIAMInfoOutput)
out.ResultMetadata = metadata
return out, nil
}
// GetIAMInfoInput provides the input parameters for GetIAMInfo operation.
type GetIAMInfoInput struct{}
// GetIAMInfoOutput provides the output parameters for GetIAMInfo operation.
type GetIAMInfoOutput struct {
IAMInfo
ResultMetadata middleware.Metadata
}
func addGetIAMInfoMiddleware(stack *middleware.Stack, options Options) error {
return addAPIRequestMiddleware(stack,
options,
buildGetIAMInfoPath,
buildGetIAMInfoOutput,
)
}
func buildGetIAMInfoPath(params interface{}) (string, error) {
return getIAMInfoPath, nil
}
func buildGetIAMInfoOutput(resp *smithyhttp.Response) (v interface{}, err error) {
defer func() {
closeErr := resp.Body.Close()
if err == nil {
err = closeErr
} else if closeErr != nil {
err = fmt.Errorf("response body close error: %v, original error: %w", closeErr, err)
}
}()
var buff [1024]byte
ringBuffer := smithyio.NewRingBuffer(buff[:])
body := io.TeeReader(resp.Body, ringBuffer)
imdsResult := &GetIAMInfoOutput{}
if err = json.NewDecoder(body).Decode(&imdsResult.IAMInfo); err != nil {
return nil, &smithy.DeserializationError{
Err: fmt.Errorf("failed to decode instance identity document, %w", err),
Snapshot: ringBuffer.Bytes(),
}
}
// Any code other success is an error
if !strings.EqualFold(imdsResult.Code, "success") {
return nil, fmt.Errorf("failed to get EC2 IMDS IAM info, %s",
imdsResult.Code)
}
return imdsResult, nil
}
// IAMInfo provides the shape for unmarshaling an IAM info from the metadata
// API.
type IAMInfo struct {
Code string
LastUpdated time.Time
InstanceProfileArn string
InstanceProfileID string
}
|