Skip to content

Commit 00a85da

Browse files
committed
refactor per feedback to authenticate access token in highlight
1 parent 25538e0 commit 00a85da

File tree

1 file changed

+9
-100
lines changed

1 file changed

+9
-100
lines changed

cmd/sourcemaps/upload.go

Lines changed: 9 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,13 @@ const (
3232
defaultPath = "."
3333
defaultBackendUrl = "https://pri.observability.app.launchdarkly.com"
3434

35-
verifyApiKeyQuery = `
36-
query LDCredential($ld_account_id: String!, $ld_project_id: String!) {
37-
ld_credential(ld_account_id: $ld_account_id, ld_project_id: $ld_project_id) {
38-
project_id
39-
api_key
40-
}
41-
}
42-
`
43-
4435
getSourceMapUrlsQuery = `
4536
query GetSourceMapUploadUrls($api_key: String!, $paths: [String!]!) {
46-
get_source_map_upload_urls(api_key: $api_key, paths: $paths)
37+
get_source_map_upload_urls_ld(
38+
api_key: String!
39+
project_id: String!
40+
paths: [String!]!
41+
): [String!]!
4742
}
4843
`
4944
)
@@ -88,35 +83,13 @@ func NewUploadCmd(client resources.Client) *cobra.Command {
8883

8984
func runE(client resources.Client) func(cmd *cobra.Command, args []string) error {
9085
return func(cmd *cobra.Command, args []string) error {
91-
u, _ := url.JoinPath(
92-
viper.GetString(cliflags.BaseURIFlag),
93-
"api/v2/caller-identity",
94-
)
95-
res, err := client.MakeRequest(
96-
viper.GetString(cliflags.AccessTokenFlag),
97-
"GET",
98-
u,
99-
"application/json",
100-
nil,
101-
nil,
102-
false,
103-
)
104-
if err != nil {
105-
return output.NewCmdOutputError(err, viper.GetString(cliflags.OutputFlag))
106-
}
107-
108-
var result struct{ AccountID string }
109-
if err = json.Unmarshal(res, &result); err != nil {
110-
return output.NewCmdOutputError(err, viper.GetString(cliflags.OutputFlag))
111-
}
112-
11386
projectKey := viper.GetString(cliflags.ProjectFlag)
114-
u, _ = url.JoinPath(
87+
u, _ := url.JoinPath(
11588
viper.GetString(cliflags.BaseURIFlag),
11689
"api/v2/projects",
11790
projectKey,
11891
)
119-
res, err = client.MakeRequest(
92+
res, err := client.MakeRequest(
12093
viper.GetString(cliflags.AccessTokenFlag),
12194
"GET",
12295
u,
@@ -148,11 +121,6 @@ func runE(client resources.Client) func(cmd *cobra.Command, args []string) error
148121
backendUrl = defaultBackendUrl
149122
}
150123

151-
highlightKey, projectID, err := verifyApiKey(result.AccountID, projectResult.ID, backendUrl)
152-
if err != nil {
153-
return fmt.Errorf("failed to verify API key: %w", err)
154-
}
155-
156124
fmt.Printf("Starting to upload source maps from %s\n", path)
157125

158126
files, err := getAllSourceMapFiles(path)
@@ -166,10 +134,10 @@ func runE(client resources.Client) func(cmd *cobra.Command, args []string) error
166134

167135
s3Keys := make([]string, 0, len(files))
168136
for _, file := range files {
169-
s3Keys = append(s3Keys, getS3Key(projectID, appVersion, basePath, file.Name))
137+
s3Keys = append(s3Keys, getS3Key(projectResult.ID, appVersion, basePath, file.Name))
170138
}
171139

172-
uploadUrls, err := getSourceMapUploadUrls(highlightKey, s3Keys, backendUrl)
140+
uploadUrls, err := getSourceMapUploadUrls(viper.GetString(cliflags.AccessTokenFlag), s3Keys, backendUrl)
173141
if err != nil {
174142
return fmt.Errorf("failed to get upload URLs: %w", err)
175143
}
@@ -185,65 +153,6 @@ func runE(client resources.Client) func(cmd *cobra.Command, args []string) error
185153
}
186154
}
187155

188-
// verifyApiKey queries the LaunchDarkly Observability API to verify credentials and retrieve the Highlight API key.
189-
// It takes the LaunchDarkly account ID, project ID, and backend URL as input.
190-
// Returns:
191-
// - string: The Highlight API key used for sourcemap uploads
192-
// - string: The Highlight project ID that was verified
193-
// - error: Any error that occurred during verification
194-
func verifyApiKey(accountID, projectID, backendUrl string) (string, string, error) {
195-
variables := map[string]string{
196-
"ld_account_id": accountID,
197-
"ld_project_id": projectID,
198-
}
199-
200-
reqBody, err := json.Marshal(map[string]interface{}{
201-
"query": verifyApiKeyQuery,
202-
"variables": variables,
203-
})
204-
if err != nil {
205-
return "", "", err
206-
}
207-
208-
req, err := http.NewRequest("POST", backendUrl, bytes.NewBuffer(reqBody))
209-
if err != nil {
210-
return "", "", err
211-
}
212-
213-
req.Header.Set("Content-Type", "application/json")
214-
215-
client := &http.Client{}
216-
resp, err := client.Do(req)
217-
if err != nil {
218-
return "", "", err
219-
}
220-
defer resp.Body.Close()
221-
222-
body, err := io.ReadAll(resp.Body)
223-
if err != nil {
224-
return "", "", err
225-
}
226-
227-
var apiKeyResp ApiKeyResponse
228-
if err := json.Unmarshal(body, &apiKeyResp); err != nil {
229-
return "", "", err
230-
}
231-
232-
if len(apiKeyResp.Errors) > 0 {
233-
return "", "", fmt.Errorf("failed to verify API key: %s", apiKeyResp.Errors[0].Message)
234-
}
235-
236-
if apiKeyResp.Data.Credential.APIKey == "" {
237-
return "", "", fmt.Errorf("invalid API key")
238-
}
239-
240-
if apiKeyResp.Data.Credential.ProjectID == "" || apiKeyResp.Data.Credential.ProjectID == "0" {
241-
return "", "", fmt.Errorf("invalid project ID")
242-
}
243-
244-
return apiKeyResp.Data.Credential.APIKey, apiKeyResp.Data.Credential.ProjectID, nil
245-
}
246-
247156
func getAllSourceMapFiles(path string) ([]SourceMapFile, error) {
248157
var files []SourceMapFile
249158
routeGroupPattern := regexp.MustCompile(`\(.+?\)/`)

0 commit comments

Comments
 (0)