-
Notifications
You must be signed in to change notification settings - Fork 3
feat: agent auth #371
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
feat: agent auth #371
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f819361
feat: agent auth
gusfcarvalho 3688e26
Merge branch 'main' into gc-feat-agent-auth
gusfcarvalho d256deb
chore: add sdk
gusfcarvalho efa1fec
fix: address copilot issues
gusfcarvalho 75c29e7
Avoid leaking agent DB errors and unblock concurrent token refresh
gusfcarvalho 281c571
Preserve SDK streaming and normalize agent auth headers
gusfcarvalho 5ca458d
Preserve streaming in SDK requests with replayable auth retries
gusfcarvalho 1ecd022
Revoke agent keys when deleting agents
gusfcarvalho File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,216 @@ | ||
| //go:build integration | ||
|
|
||
| package handler | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "context" | ||
| "encoding/json" | ||
| "fmt" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/compliance-framework/api/internal/api" | ||
| "github.com/compliance-framework/api/internal/service/relational" | ||
| "github.com/compliance-framework/api/internal/tests" | ||
| "github.com/labstack/echo/v4" | ||
| "github.com/stretchr/testify/require" | ||
| "github.com/stretchr/testify/suite" | ||
| "go.uber.org/zap" | ||
| ) | ||
|
|
||
| type AgentAPIIntegrationSuite struct { | ||
| tests.IntegrationTestSuite | ||
| server *api.Server | ||
| } | ||
|
|
||
| func TestAgentAPI(t *testing.T) { | ||
| suite.Run(t, new(AgentAPIIntegrationSuite)) | ||
| } | ||
|
|
||
| func (suite *AgentAPIIntegrationSuite) SetupTest() { | ||
| err := suite.Migrator.Refresh() | ||
| suite.Require().NoError(err) | ||
|
|
||
| logger, _ := zap.NewDevelopment() | ||
| metrics := api.NewMetricsHandler(context.Background(), logger.Sugar()) | ||
| suite.server = api.NewServer(context.Background(), logger.Sugar(), suite.Config, metrics) | ||
| RegisterHandlers(suite.server, logger.Sugar(), suite.DB, suite.Config, &APIServices{}) | ||
| } | ||
|
|
||
| func (suite *AgentAPIIntegrationSuite) authedRequest(method, path string, body any) (*httptest.ResponseRecorder, *http.Request) { | ||
| token, err := suite.GetAuthToken() | ||
| suite.Require().NoError(err) | ||
|
|
||
| payload := []byte{} | ||
| if body != nil { | ||
| data, marshalErr := json.Marshal(body) | ||
| suite.Require().NoError(marshalErr) | ||
| payload = data | ||
| } | ||
| rec := httptest.NewRecorder() | ||
| req := httptest.NewRequest(method, path, bytes.NewReader(payload)) | ||
| req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON) | ||
| req.Header.Set(echo.HeaderAuthorization, fmt.Sprintf("Bearer %s", *token)) | ||
| return rec, req | ||
| } | ||
|
|
||
| func (suite *AgentAPIIntegrationSuite) TestAgentCRUDAndKeys() { | ||
| createRec, createReq := suite.authedRequest(http.MethodPost, "/api/admin/agents", map[string]any{ | ||
| "name": "agent-one", | ||
| "description": "integration agent", | ||
| }) | ||
| suite.server.E().ServeHTTP(createRec, createReq) | ||
| require.Equal(suite.T(), http.StatusCreated, createRec.Code) | ||
|
|
||
| var created GenericDataResponse[agentResponse] | ||
| require.NoError(suite.T(), json.Unmarshal(createRec.Body.Bytes(), &created)) | ||
| require.Equal(suite.T(), "agent-one", created.Data.Name) | ||
| require.Equal(suite.T(), int64(0), created.Data.ServiceAccountKeys) | ||
|
|
||
| listRec, listReq := suite.authedRequest(http.MethodGet, "/api/admin/agents", nil) | ||
| suite.server.E().ServeHTTP(listRec, listReq) | ||
| require.Equal(suite.T(), http.StatusOK, listRec.Code) | ||
|
|
||
| var listed GenericDataListResponse[agentResponse] | ||
| require.NoError(suite.T(), json.Unmarshal(listRec.Body.Bytes(), &listed)) | ||
| require.Len(suite.T(), listed.Data, 1) | ||
|
|
||
| getRec, getReq := suite.authedRequest(http.MethodGet, fmt.Sprintf("/api/admin/agents/%s", created.Data.ID), nil) | ||
| suite.server.E().ServeHTTP(getRec, getReq) | ||
| require.Equal(suite.T(), http.StatusOK, getRec.Code) | ||
|
|
||
| updateRec, updateReq := suite.authedRequest(http.MethodPut, fmt.Sprintf("/api/admin/agents/%s", created.Data.ID), map[string]any{ | ||
| "name": "agent-one-updated", | ||
| "is-active": false, | ||
| }) | ||
| suite.server.E().ServeHTTP(updateRec, updateReq) | ||
| require.Equal(suite.T(), http.StatusOK, updateRec.Code) | ||
|
|
||
| var updated GenericDataResponse[agentResponse] | ||
| require.NoError(suite.T(), json.Unmarshal(updateRec.Body.Bytes(), &updated)) | ||
| require.Equal(suite.T(), "agent-one-updated", updated.Data.Name) | ||
| require.False(suite.T(), updated.Data.IsActive) | ||
|
|
||
| keyCreateRec, keyCreateReq := suite.authedRequest(http.MethodPost, fmt.Sprintf("/api/admin/agents/%s/keys", created.Data.ID), map[string]any{ | ||
| "name": "primary", | ||
| "never-expires": true, | ||
| }) | ||
| suite.server.E().ServeHTTP(keyCreateRec, keyCreateReq) | ||
| require.Equal(suite.T(), http.StatusCreated, keyCreateRec.Code) | ||
|
|
||
| var keyCreated GenericDataResponse[agentKeyCreateResponse] | ||
| require.NoError(suite.T(), json.Unmarshal(keyCreateRec.Body.Bytes(), &keyCreated)) | ||
| require.NotEmpty(suite.T(), keyCreated.Data.ClientID) | ||
| require.NotEmpty(suite.T(), keyCreated.Data.ClientSecret) | ||
| require.True(suite.T(), keyCreated.Data.NeverExpires) | ||
|
|
||
| keyListRec, keyListReq := suite.authedRequest(http.MethodGet, fmt.Sprintf("/api/admin/agents/%s/keys", created.Data.ID), nil) | ||
| suite.server.E().ServeHTTP(keyListRec, keyListReq) | ||
| require.Equal(suite.T(), http.StatusOK, keyListRec.Code) | ||
|
|
||
| var keyList GenericDataListResponse[agentKeyResponse] | ||
| require.NoError(suite.T(), json.Unmarshal(keyListRec.Body.Bytes(), &keyList)) | ||
| require.Len(suite.T(), keyList.Data, 1) | ||
| require.Equal(suite.T(), keyCreated.Data.ClientID, keyList.Data[0].ClientID) | ||
| require.True(suite.T(), keyList.Data[0].NeverExpires) | ||
|
|
||
| keyGetRec, keyGetReq := suite.authedRequest(http.MethodGet, fmt.Sprintf("/api/admin/agents/%s/keys/%s", created.Data.ID, keyCreated.Data.ID), nil) | ||
| suite.server.E().ServeHTTP(keyGetRec, keyGetReq) | ||
| require.Equal(suite.T(), http.StatusOK, keyGetRec.Code) | ||
|
|
||
| keyDeleteRec, keyDeleteReq := suite.authedRequest(http.MethodDelete, fmt.Sprintf("/api/admin/agents/%s/keys/%s", created.Data.ID, keyCreated.Data.ID), nil) | ||
| suite.server.E().ServeHTTP(keyDeleteRec, keyDeleteReq) | ||
| require.Equal(suite.T(), http.StatusNoContent, keyDeleteRec.Code) | ||
|
|
||
| deleteRec, deleteReq := suite.authedRequest(http.MethodDelete, fmt.Sprintf("/api/admin/agents/%s", created.Data.ID), nil) | ||
| suite.server.E().ServeHTTP(deleteRec, deleteReq) | ||
| require.Equal(suite.T(), http.StatusNoContent, deleteRec.Code) | ||
| } | ||
|
|
||
| func (suite *AgentAPIIntegrationSuite) TestCreateAgentKeyWithExpiry() { | ||
| err := suite.Migrator.Refresh() | ||
| suite.Require().NoError(err) | ||
|
|
||
| createRec, createReq := suite.authedRequest(http.MethodPost, "/api/admin/agents", map[string]any{ | ||
| "name": "agent-two", | ||
| }) | ||
| suite.server.E().ServeHTTP(createRec, createReq) | ||
| require.Equal(suite.T(), http.StatusCreated, createRec.Code) | ||
|
|
||
| var created GenericDataResponse[agentResponse] | ||
| require.NoError(suite.T(), json.Unmarshal(createRec.Body.Bytes(), &created)) | ||
|
|
||
| expiresAt := time.Now().UTC().Add(2 * time.Hour).Format(time.RFC3339) | ||
| keyCreateRec, keyCreateReq := suite.authedRequest(http.MethodPost, fmt.Sprintf("/api/admin/agents/%s/keys", created.Data.ID), map[string]any{ | ||
| "name": "expiring", | ||
| "expires-at": expiresAt, | ||
| }) | ||
| suite.server.E().ServeHTTP(keyCreateRec, keyCreateReq) | ||
| require.Equal(suite.T(), http.StatusCreated, keyCreateRec.Code) | ||
|
|
||
| var keyCreated GenericDataResponse[agentKeyCreateResponse] | ||
| require.NoError(suite.T(), json.Unmarshal(keyCreateRec.Body.Bytes(), &keyCreated)) | ||
| require.False(suite.T(), keyCreated.Data.NeverExpires) | ||
| require.NotNil(suite.T(), keyCreated.Data.ExpiresAt) | ||
| } | ||
|
|
||
| func (suite *AgentAPIIntegrationSuite) TestCreateAgentKeyRequiresExplicitExpiryDecision() { | ||
| err := suite.Migrator.Refresh() | ||
| suite.Require().NoError(err) | ||
|
|
||
| createRec, createReq := suite.authedRequest(http.MethodPost, "/api/admin/agents", map[string]any{ | ||
| "name": "agent-three", | ||
| }) | ||
| suite.server.E().ServeHTTP(createRec, createReq) | ||
| require.Equal(suite.T(), http.StatusCreated, createRec.Code) | ||
|
|
||
| var created GenericDataResponse[agentResponse] | ||
| require.NoError(suite.T(), json.Unmarshal(createRec.Body.Bytes(), &created)) | ||
|
|
||
| keyCreateRec, keyCreateReq := suite.authedRequest(http.MethodPost, fmt.Sprintf("/api/admin/agents/%s/keys", created.Data.ID), map[string]any{ | ||
| "name": "missing-expiry-choice", | ||
| }) | ||
| suite.server.E().ServeHTTP(keyCreateRec, keyCreateReq) | ||
| require.Equal(suite.T(), http.StatusBadRequest, keyCreateRec.Code) | ||
| require.Contains(suite.T(), keyCreateRec.Body.String(), "expires-at is required unless never-expires is true") | ||
| } | ||
|
|
||
| func (suite *AgentAPIIntegrationSuite) TestDeleteAgentRevokesKeysAndDeactivatesAgent() { | ||
| createRec, createReq := suite.authedRequest(http.MethodPost, "/api/admin/agents", map[string]any{ | ||
| "name": "agent-delete-test", | ||
| }) | ||
| suite.server.E().ServeHTTP(createRec, createReq) | ||
| require.Equal(suite.T(), http.StatusCreated, createRec.Code) | ||
|
|
||
| var created GenericDataResponse[agentResponse] | ||
| require.NoError(suite.T(), json.Unmarshal(createRec.Body.Bytes(), &created)) | ||
|
|
||
| keyCreateRec, keyCreateReq := suite.authedRequest(http.MethodPost, fmt.Sprintf("/api/admin/agents/%s/keys", created.Data.ID), map[string]any{ | ||
| "name": "primary", | ||
| "never-expires": true, | ||
| }) | ||
| suite.server.E().ServeHTTP(keyCreateRec, keyCreateReq) | ||
| require.Equal(suite.T(), http.StatusCreated, keyCreateRec.Code) | ||
|
|
||
| var keyCreated GenericDataResponse[agentKeyCreateResponse] | ||
| require.NoError(suite.T(), json.Unmarshal(keyCreateRec.Body.Bytes(), &keyCreated)) | ||
|
|
||
| deleteRec, deleteReq := suite.authedRequest(http.MethodDelete, fmt.Sprintf("/api/admin/agents/%s", created.Data.ID), nil) | ||
| suite.server.E().ServeHTTP(deleteRec, deleteReq) | ||
| require.Equal(suite.T(), http.StatusNoContent, deleteRec.Code) | ||
|
|
||
| var agent relational.Agent | ||
| err := suite.DB.Unscoped().First(&agent, "id = ?", created.Data.ID).Error | ||
| require.NoError(suite.T(), err) | ||
| require.False(suite.T(), agent.IsActive) | ||
| require.NotNil(suite.T(), agent.DeletedAt) | ||
| require.True(suite.T(), agent.DeletedAt.Valid) | ||
|
|
||
| var key relational.AgentServiceAccountKey | ||
| err = suite.DB.First(&key, "id = ?", keyCreated.Data.ID).Error | ||
| require.NoError(suite.T(), err) | ||
| require.NotNil(suite.T(), key.RevokedAt) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.