|
6 | 6 | "encoding/json" |
7 | 7 | "fmt" |
8 | 8 | "io" |
9 | | - "io/ioutil" |
10 | 9 | "log" |
11 | 10 | "net/http" |
12 | 11 | "os" |
@@ -58,22 +57,30 @@ func createHTTPClient() *http.Client { |
58 | 57 | return client |
59 | 58 | } |
60 | 59 |
|
| 60 | +// handleHTTPResponse checks the HTTP response status and handles errors appropriately. |
61 | 61 | func handleHTTPResponse(resp *http.Response) error { |
62 | | - if resp.StatusCode == http.StatusForbidden { |
63 | | - log.Println("🚫 Not authorized. Token expired or revoked.") |
64 | | - return nil |
65 | | - } |
66 | | - |
67 | | - if resp.StatusCode != http.StatusOK { |
68 | | - body, err := io.ReadAll(resp.Body) |
69 | | - if err != nil { |
70 | | - return fmt.Errorf("failed to read response body: %v", err) |
71 | | - } |
72 | | - errorMessage := fmt.Sprintf("🗿 Request failed with status code %d: %s", resp.StatusCode, string(body)) |
73 | | - return fmt.Errorf(errorMessage) |
74 | | - } |
75 | | - |
76 | | - return nil |
| 62 | + switch resp.StatusCode { |
| 63 | + case http.StatusOK: |
| 64 | + // If OK, nothing more to do. |
| 65 | + return nil |
| 66 | + case http.StatusForbidden: |
| 67 | + // Handle forbidden access. |
| 68 | + log.Println("🚫 Not authorized. Token expired or revoked.") |
| 69 | + return nil |
| 70 | + case http.StatusTooManyRequests: |
| 71 | + // Handle rate limiting. |
| 72 | + retryAfter := resp.Header.Get("Retry-After") |
| 73 | + log.Printf("⏳ Rate limit exceeded. Retry after %s seconds.", retryAfter) |
| 74 | + return fmt.Errorf("rate limit exceeded, retry after %s seconds", retryAfter) |
| 75 | + default: |
| 76 | + // Handle other unexpected statuses. |
| 77 | + body, err := io.ReadAll(resp.Body) |
| 78 | + if err != nil { |
| 79 | + return fmt.Errorf("failed to read response body: %v", err) |
| 80 | + } |
| 81 | + errorMessage := fmt.Sprintf("🗿 Request failed with status code %d: %s", resp.StatusCode, string(body)) |
| 82 | + return fmt.Errorf(errorMessage) |
| 83 | + } |
77 | 84 | } |
78 | 85 |
|
79 | 86 | func FetchPhaseUser(appToken, host string) (*http.Response, error) { |
@@ -149,51 +156,6 @@ func FetchAppKey(appToken, host string) (string, error) { |
149 | 156 | return jsonResp.WrappedKeyShare, nil |
150 | 157 | } |
151 | 158 |
|
152 | | -// FetchWrappedKeyShare fetches the wrapped application key share from Phase KMS. |
153 | | -func FetchWrappedKeyShare(appToken, host string) (string, error) { |
154 | | - client := &http.Client{} |
155 | | - |
156 | | - // Check if SSL verification should be skipped |
157 | | - if !misc.VerifySSL { |
158 | | - client.Transport = &http.Transport{ |
159 | | - TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, |
160 | | - } |
161 | | - } |
162 | | - |
163 | | - url := fmt.Sprintf("%s/service/secrets/tokens/", host) |
164 | | - req, err := http.NewRequest("GET", url, nil) |
165 | | - if err != nil { |
166 | | - return "", err |
167 | | - } |
168 | | - req.Header = ConstructHTTPHeaders(appToken) |
169 | | - |
170 | | - resp, err := client.Do(req) |
171 | | - if err != nil { |
172 | | - return "", fmt.Errorf("network error: please check your internet connection. Detail: %v", err) |
173 | | - } |
174 | | - defer resp.Body.Close() |
175 | | - |
176 | | - if resp.StatusCode != http.StatusOK { |
177 | | - body, err := ioutil.ReadAll(resp.Body) |
178 | | - if err != nil { |
179 | | - return "", fmt.Errorf("request failed with status code %d: failed to read response body", resp.StatusCode) |
180 | | - } |
181 | | - return "", fmt.Errorf("request failed with status code %d: %s", resp.StatusCode, string(body)) |
182 | | - } |
183 | | - |
184 | | - var jsonResp map[string]string |
185 | | - if err := json.NewDecoder(resp.Body).Decode(&jsonResp); err != nil { |
186 | | - return "", fmt.Errorf("failed to decode JSON from response: %v", err) |
187 | | - } |
188 | | - |
189 | | - wrappedKeyShare, ok := jsonResp["wrapped_key_share"] |
190 | | - if !ok { |
191 | | - return "", fmt.Errorf("wrapped key share not found in the response") |
192 | | - } |
193 | | - |
194 | | - return wrappedKeyShare, nil |
195 | | -} |
196 | | - |
197 | 159 | func FetchPhaseSecrets(appToken, environmentID, host, path string) ([]map[string]interface{}, error) { |
198 | 160 | client := createHTTPClient() |
199 | 161 | url := fmt.Sprintf("%s/service/secrets/", host) |
|
0 commit comments