mirror of https://github.com/grafana/grafana
Cloudwatch: Upgrade aws-sdk and display external ids for temporary credentials (#72821)
(under a feature toggle, not yet ready for public testing)pull/73252/head
parent
c2aeb9882d
commit
09d5483c6c
@ -0,0 +1,29 @@ |
||||
package routes |
||||
|
||||
import ( |
||||
"context" |
||||
"encoding/json" |
||||
"net/http" |
||||
"net/url" |
||||
"os" |
||||
|
||||
"github.com/grafana/grafana-aws-sdk/pkg/awsds" |
||||
"github.com/grafana/grafana-plugin-sdk-go/backend" |
||||
"github.com/grafana/grafana/pkg/tsdb/cloudwatch/models" |
||||
) |
||||
|
||||
type ExternalIdResponse struct { |
||||
ExternalId string `json:"externalId"` |
||||
} |
||||
|
||||
func ExternalIdHandler(ctx context.Context, pluginCtx backend.PluginContext, reqCtxFactory models.RequestContextFactoryFunc, parameters url.Values) ([]byte, *models.HttpError) { |
||||
response := ExternalIdResponse{ |
||||
ExternalId: os.Getenv(awsds.GrafanaAssumeRoleExternalIdKeyName), |
||||
} |
||||
jsonResponse, err := json.Marshal(response) |
||||
if err != nil { |
||||
return nil, models.NewHttpError("error in ExternalIdHandler", http.StatusInternalServerError, err) |
||||
} |
||||
|
||||
return jsonResponse, nil |
||||
} |
@ -0,0 +1,36 @@ |
||||
package routes |
||||
|
||||
import ( |
||||
"net/http" |
||||
"net/http/httptest" |
||||
"testing" |
||||
|
||||
"github.com/stretchr/testify/assert" |
||||
) |
||||
|
||||
func Test_external_id_route(t *testing.T) { |
||||
t.Run("successfully returns an external id from the env", func(t *testing.T) { |
||||
t.Setenv("AWS_AUTH_EXTERNAL_ID", "mock-external-id") |
||||
rr := httptest.NewRecorder() |
||||
|
||||
handler := http.HandlerFunc(ResourceRequestMiddleware(ExternalIdHandler, logger, nil)) |
||||
req := httptest.NewRequest("GET", "/external-id", nil) |
||||
|
||||
handler.ServeHTTP(rr, req) |
||||
|
||||
assert.Equal(t, http.StatusOK, rr.Code) |
||||
assert.JSONEq(t, `{"externalId":"mock-external-id"}`, rr.Body.String()) |
||||
}) |
||||
|
||||
t.Run("returns an empty string if there is no external id", func(t *testing.T) { |
||||
rr := httptest.NewRecorder() |
||||
|
||||
handler := http.HandlerFunc(ResourceRequestMiddleware(ExternalIdHandler, logger, nil)) |
||||
req := httptest.NewRequest("GET", "/external-id", nil) |
||||
|
||||
handler.ServeHTTP(rr, req) |
||||
|
||||
assert.Equal(t, http.StatusOK, rr.Code) |
||||
assert.JSONEq(t, `{"externalId":""}`, rr.Body.String()) |
||||
}) |
||||
} |
Loading…
Reference in new issue