|
| 1 | +/* |
| 2 | + * MinIO Go Library for Amazon S3 Compatible Cloud Storage |
| 3 | + * Copyright 2015-2025 MinIO, Inc. |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package minio |
| 19 | + |
| 20 | +import ( |
| 21 | + "context" |
| 22 | + "encoding/xml" |
| 23 | + "errors" |
| 24 | + "net" |
| 25 | + "net/http" |
| 26 | + "net/url" |
| 27 | + "path" |
| 28 | + "time" |
| 29 | + |
| 30 | + "github.com/minio/minio-go/v7/pkg/credentials" |
| 31 | + "github.com/minio/minio-go/v7/pkg/s3utils" |
| 32 | + "github.com/minio/minio-go/v7/pkg/signer" |
| 33 | +) |
| 34 | + |
| 35 | +// SessionMode - session mode type there are only two types |
| 36 | +type SessionMode string |
| 37 | + |
| 38 | +// Session constants |
| 39 | +const ( |
| 40 | + SessionReadWrite SessionMode = "ReadWrite" |
| 41 | + SessionReadOnly SessionMode = "ReadOnly" |
| 42 | +) |
| 43 | + |
| 44 | +type createSessionResult struct { |
| 45 | + XMLName xml.Name `xml:"http://s3.amazonaws.com/doc/2006-03-01/ CreateSessionResult"` |
| 46 | + Credentials struct { |
| 47 | + AccessKey string `xml:"AccessKeyId" json:"accessKey,omitempty"` |
| 48 | + SecretKey string `xml:"SecretAccessKey" json:"secretKey,omitempty"` |
| 49 | + SessionToken string `xml:"SessionToken" json:"sessionToken,omitempty"` |
| 50 | + Expiration time.Time `xml:"Expiration" json:"expiration,omitempty"` |
| 51 | + } `xml:",omitempty"` |
| 52 | +} |
| 53 | + |
| 54 | +// CreateSession - https://docs.aws.amazon.com/AmazonS3/latest/API/API_CreateSession.html |
| 55 | +// the returning credentials may be cached depending on the expiration of the original |
| 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 | + credSession := &createSessionResult{} |
| 82 | + dec := xml.NewDecoder(resp.Body) |
| 83 | + if err = dec.Decode(credSession); err != nil { |
| 84 | + return credentials.Value{}, err |
| 85 | + } |
| 86 | + |
| 87 | + defer c.bucketSessionCache.Set(bucketName, cred) |
| 88 | + |
| 89 | + return credentials.Value{ |
| 90 | + AccessKeyID: credSession.Credentials.AccessKey, |
| 91 | + SecretAccessKey: credSession.Credentials.SecretKey, |
| 92 | + SessionToken: credSession.Credentials.SessionToken, |
| 93 | + Expiration: credSession.Credentials.Expiration, |
| 94 | + }, nil |
| 95 | +} |
| 96 | + |
| 97 | +// createSessionRequest - Wrapper creates a new CreateSession request. |
| 98 | +func (c *Client) createSessionRequest(ctx context.Context, bucketName string, sessionMode SessionMode) (*http.Request, error) { |
| 99 | + // Set location query. |
| 100 | + urlValues := make(url.Values) |
| 101 | + urlValues.Set("session", "") |
| 102 | + |
| 103 | + // Set get bucket location always as path style. |
| 104 | + targetURL := *c.endpointURL |
| 105 | + |
| 106 | + // as it works in makeTargetURL method from api.go file |
| 107 | + if h, p, err := net.SplitHostPort(targetURL.Host); err == nil { |
| 108 | + if targetURL.Scheme == "http" && p == "80" || targetURL.Scheme == "https" && p == "443" { |
| 109 | + targetURL.Host = h |
| 110 | + if ip := net.ParseIP(h); ip != nil && ip.To16() != nil { |
| 111 | + targetURL.Host = "[" + h + "]" |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + isVirtualStyle := c.isVirtualHostStyleRequest(targetURL, bucketName) |
| 117 | + |
| 118 | + var urlStr string |
| 119 | + |
| 120 | + if isVirtualStyle { |
| 121 | + urlStr = c.endpointURL.Scheme + "://" + bucketName + "." + targetURL.Host + "/?session" |
| 122 | + } else { |
| 123 | + targetURL.Path = path.Join(bucketName, "") + "/" |
| 124 | + targetURL.RawQuery = urlValues.Encode() |
| 125 | + urlStr = targetURL.String() |
| 126 | + } |
| 127 | + |
| 128 | + // Get a new HTTP request for the method. |
| 129 | + req, err := http.NewRequestWithContext(ctx, http.MethodGet, urlStr, nil) |
| 130 | + if err != nil { |
| 131 | + return nil, err |
| 132 | + } |
| 133 | + |
| 134 | + // Set UserAgent for the request. |
| 135 | + c.setUserAgent(req) |
| 136 | + |
| 137 | + // Get credentials from the configured credentials provider. |
| 138 | + value, err := c.credsProvider.GetWithContext(c.CredContext()) |
| 139 | + if err != nil { |
| 140 | + return nil, err |
| 141 | + } |
| 142 | + |
| 143 | + var ( |
| 144 | + signerType = value.SignerType |
| 145 | + accessKeyID = value.AccessKeyID |
| 146 | + secretAccessKey = value.SecretAccessKey |
| 147 | + sessionToken = value.SessionToken |
| 148 | + ) |
| 149 | + |
| 150 | + // Custom signer set then override the behavior. |
| 151 | + if c.overrideSignerType != credentials.SignatureDefault { |
| 152 | + signerType = c.overrideSignerType |
| 153 | + } |
| 154 | + |
| 155 | + // If signerType returned by credentials helper is anonymous, |
| 156 | + // then do not sign regardless of signerType override. |
| 157 | + if value.SignerType == credentials.SignatureAnonymous { |
| 158 | + signerType = credentials.SignatureAnonymous |
| 159 | + } |
| 160 | + |
| 161 | + if signerType.IsAnonymous() || signerType.IsV2() { |
| 162 | + return req, errors.New("Only signature v4 is supported for CreateSession() API") |
| 163 | + } |
| 164 | + |
| 165 | + // Set sha256 sum for signature calculation only with signature version '4'. |
| 166 | + contentSha256 := emptySHA256Hex |
| 167 | + if c.secure { |
| 168 | + contentSha256 = unsignedPayload |
| 169 | + } |
| 170 | + |
| 171 | + req.Header.Set("X-Amz-Content-Sha256", contentSha256) |
| 172 | + req.Header.Set("x-amz-create-session-mode", string(sessionMode)) |
| 173 | + req = signer.SignV4(*req, accessKeyID, secretAccessKey, sessionToken, c.region) |
| 174 | + return req, nil |
| 175 | +} |
0 commit comments