CreateSession - https://docs.aws.amazon.com/AmazonS3/latest/API/API_CreateSession.html the returning credentials may be cached depending on the expiration of the original credential, credentials will get renewed 10 secs earlier than when its gonna expire allowing for some leeway in the renewal proce
(ctx context.Context, bucketName string, sessionMode SessionMode)
| 56 | // credential, credentials will get renewed 10 secs earlier than when its gonna expire |
| 57 | // allowing for some leeway in the renewal process. |
| 58 | func (c *Client) CreateSession(ctx context.Context, bucketName string, sessionMode SessionMode) (cred credentials.Value, err error) { |
| 59 | if err := s3utils.CheckValidBucketNameS3Express(bucketName); err != nil { |
| 60 | return credentials.Value{}, err |
| 61 | } |
| 62 | |
| 63 | v, ok := c.bucketSessionCache.Get(bucketName) |
| 64 | if ok && v.Expiration.After(time.Now().Add(10*time.Second)) { |
| 65 | // Verify if the credentials will not expire |
| 66 | // in another 10 seconds, if not we renew it again. |
| 67 | return v, nil |
| 68 | } |
| 69 | |
| 70 | req, err := c.createSessionRequest(ctx, bucketName, sessionMode) |
| 71 | if err != nil { |
| 72 | return credentials.Value{}, err |
| 73 | } |
| 74 | |
| 75 | resp, err := c.do(req) |
| 76 | defer closeResponse(resp) |
| 77 | if err != nil { |
| 78 | return credentials.Value{}, err |
| 79 | } |
| 80 | |
| 81 | if resp.StatusCode != http.StatusOK { |
| 82 | return credentials.Value{}, httpRespToErrorResponse(resp, bucketName, "") |
| 83 | } |
| 84 | |
| 85 | credSession := &createSessionResult{} |
| 86 | dec := xml.NewDecoder(resp.Body) |
| 87 | if err = dec.Decode(credSession); err != nil { |
| 88 | return credentials.Value{}, err |
| 89 | } |
| 90 | |
| 91 | defer c.bucketSessionCache.Set(bucketName, cred) |
| 92 | |
| 93 | return credentials.Value{ |
| 94 | AccessKeyID: credSession.Credentials.AccessKey, |
| 95 | SecretAccessKey: credSession.Credentials.SecretKey, |
| 96 | SessionToken: credSession.Credentials.SessionToken, |
| 97 | Expiration: credSession.Credentials.Expiration, |
| 98 | }, nil |
| 99 | } |
| 100 | |
| 101 | // createSessionRequest - Wrapper creates a new CreateSession request. |
| 102 | func (c *Client) createSessionRequest(ctx context.Context, bucketName string, sessionMode SessionMode) (*http.Request, error) { |
no test coverage detected