-
-
Notifications
You must be signed in to change notification settings - Fork 186
fix(server): keep service accounts out of the namespace payload #7129
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
Changes from all commits
Commits
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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
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,94 @@ | ||
| package responses | ||
|
|
||
| import ( | ||
| "time" | ||
|
|
||
| "github.com/shellhub-io/shellhub/pkg/api/authorizer" | ||
| "github.com/shellhub-io/shellhub/pkg/models" | ||
| ) | ||
|
|
||
| // Member is a person's membership as the namespace routes return it. It carries no principal | ||
| // type, because every member here is a person. | ||
| type Member struct { | ||
| ID string `json:"id,omitempty"` | ||
| AddedAt time.Time `json:"added_at"` | ||
| Email string `json:"email"` | ||
| Role authorizer.Role `json:"role"` | ||
| AccountStatus models.UserStatus `json:"account_status,omitempty"` | ||
| AwaitingApproval bool `json:"awaiting_approval,omitempty"` | ||
| } | ||
|
|
||
| // Namespace is what the namespace routes return. Its Members holds people only; a service | ||
| // account is listed by GET /api/service-accounts instead. | ||
| type Namespace struct { | ||
| Name string `json:"name"` | ||
| Owner string `json:"owner"` | ||
| TenantID string `json:"tenant_id"` | ||
| Members []Member `json:"members"` | ||
| Settings *models.NamespaceSettings `json:"settings"` | ||
|
|
||
| DevicesAcceptedCount int64 `json:"devices_accepted_count"` | ||
| DevicesPendingCount int64 `json:"devices_pending_count"` | ||
| DevicesRejectedCount int64 `json:"devices_rejected_count"` | ||
| DevicesRemovedCount int64 `json:"devices_removed_count"` | ||
|
|
||
| MaxDevices int `json:"max_devices"` | ||
| CreatedAt time.Time `json:"created_at"` | ||
| Billing *models.Billing `json:"billing"` | ||
| Type models.Type `json:"type"` | ||
| } | ||
|
|
||
| // NamespaceFromModel projects the stored namespace onto the response, dropping the service | ||
| // accounts from its members. It returns nil for a nil namespace, and its Members is never nil, so | ||
| // a namespace with no human member serializes as an empty array rather than null. A member whose | ||
| // type is empty counts as a person. | ||
| func NamespaceFromModel(m *models.Namespace) *Namespace { | ||
| if m == nil { | ||
| return nil | ||
| } | ||
|
|
||
| members := make([]Member, 0, len(m.Members)) | ||
|
|
||
| for _, member := range m.Members { | ||
| if member.Type == models.UserTypeService { | ||
| continue | ||
| } | ||
|
|
||
| members = append(members, Member{ | ||
| ID: member.ID, | ||
| AddedAt: member.AddedAt, | ||
| Email: member.Email, | ||
| Role: member.Role, | ||
| AccountStatus: member.AccountStatus, | ||
| AwaitingApproval: member.AwaitingApproval, | ||
| }) | ||
| } | ||
|
|
||
| return &Namespace{ | ||
| Name: m.Name, | ||
| Owner: m.Owner, | ||
| TenantID: m.TenantID, | ||
| Members: members, | ||
| Settings: m.Settings, | ||
| DevicesAcceptedCount: m.DevicesAcceptedCount, | ||
| DevicesPendingCount: m.DevicesPendingCount, | ||
| DevicesRejectedCount: m.DevicesRejectedCount, | ||
| DevicesRemovedCount: m.DevicesRemovedCount, | ||
| MaxDevices: m.MaxDevices, | ||
| CreatedAt: m.CreatedAt, | ||
| Billing: m.Billing, | ||
| Type: m.Type, | ||
| } | ||
| } | ||
|
|
||
| // NamespacesFromModel projects a page of namespaces, and returns an empty slice for an empty or | ||
| // nil page so the list route never serializes null. | ||
| func NamespacesFromModel(m []models.Namespace) []Namespace { | ||
| namespaces := make([]Namespace, 0, len(m)) | ||
|
|
||
| for _, namespace := range m { | ||
| namespaces = append(namespaces, *NamespaceFromModel(&namespace)) | ||
| } | ||
|
|
||
| return namespaces | ||
| } | ||
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,218 @@ | ||
| package responses_test | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/shellhub-io/shellhub/pkg/api/authorizer" | ||
| "github.com/shellhub-io/shellhub/pkg/api/responses" | ||
| "github.com/shellhub-io/shellhub/pkg/models" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestNamespaceFromModel(t *testing.T) { | ||
| cases := []struct { | ||
| description string | ||
| namespace *models.Namespace | ||
| expected []responses.Member | ||
| }{ | ||
| { | ||
| description: "drops the service account", | ||
| namespace: &models.Namespace{ | ||
| Members: []models.Member{ | ||
| {ID: "human", Email: "human@test.com", Role: authorizer.RoleOwner}, | ||
| {ID: "bot", Email: "bot@test.com", Role: authorizer.RoleService, Type: models.UserTypeService}, | ||
| }, | ||
| }, | ||
| expected: []responses.Member{ | ||
| {ID: "human", Email: "human@test.com", Role: authorizer.RoleOwner}, | ||
| }, | ||
| }, | ||
| { | ||
| description: "drops a service account that does not carry the service role", | ||
| namespace: &models.Namespace{ | ||
| Members: []models.Member{ | ||
| {ID: "human", Email: "human@test.com", Role: authorizer.RoleOwner, Type: models.UserTypeHuman}, | ||
| {ID: "bot", Email: "bot@test.com", Role: authorizer.RoleObserver, Type: models.UserTypeService}, | ||
| }, | ||
| }, | ||
| expected: []responses.Member{ | ||
| {ID: "human", Email: "human@test.com", Role: authorizer.RoleOwner}, | ||
| }, | ||
| }, | ||
| { | ||
| description: "keeps a member whose type is empty", | ||
| namespace: &models.Namespace{ | ||
| Members: []models.Member{ | ||
| {ID: "legacy", Email: "legacy@test.com", Role: authorizer.RoleObserver}, | ||
| }, | ||
| }, | ||
| expected: []responses.Member{ | ||
| {ID: "legacy", Email: "legacy@test.com", Role: authorizer.RoleObserver}, | ||
| }, | ||
| }, | ||
| { | ||
| description: "keeps a member whose type is human", | ||
| namespace: &models.Namespace{ | ||
| Members: []models.Member{ | ||
| {ID: "human", Email: "human@test.com", Role: authorizer.RoleOperator, Type: models.UserTypeHuman}, | ||
| }, | ||
| }, | ||
| expected: []responses.Member{ | ||
| {ID: "human", Email: "human@test.com", Role: authorizer.RoleOperator}, | ||
| }, | ||
| }, | ||
| { | ||
| description: "projects an empty list when every member is a service account", | ||
| namespace: &models.Namespace{ | ||
| Members: []models.Member{ | ||
| {ID: "bot", Email: "bot@test.com", Role: authorizer.RoleService, Type: models.UserTypeService}, | ||
| }, | ||
| }, | ||
| expected: []responses.Member{}, | ||
| }, | ||
| { | ||
| description: "projects an empty list when the namespace has no members", | ||
| namespace: &models.Namespace{}, | ||
| expected: []responses.Member{}, | ||
| }, | ||
| } | ||
|
|
||
| for _, tc := range cases { | ||
| t.Run(tc.description, func(t *testing.T) { | ||
| res := responses.NamespaceFromModel(tc.namespace) | ||
|
|
||
| require.NotNil(t, res) | ||
| assert.Equal(t, tc.expected, res.Members) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestNamespaceFromModelNil(t *testing.T) { | ||
| assert.Nil(t, responses.NamespaceFromModel(nil)) | ||
| } | ||
|
|
||
| func TestNamespaceFromModelNeverMarshalsMembersAsNull(t *testing.T) { | ||
| data, err := json.Marshal(responses.NamespaceFromModel(&models.Namespace{})) | ||
| require.NoError(t, err) | ||
|
|
||
| decoded := map[string]json.RawMessage{} | ||
| require.NoError(t, json.Unmarshal(data, &decoded)) | ||
|
|
||
| assert.JSONEq(t, "[]", string(decoded["members"])) | ||
| } | ||
|
|
||
| func TestNamespaceFromModelDoesNotMutateSource(t *testing.T) { | ||
| namespace := &models.Namespace{ | ||
| Members: []models.Member{ | ||
| {ID: "human", Email: "human@test.com", Role: authorizer.RoleOwner}, | ||
| {ID: "bot", Email: "bot@test.com", Role: authorizer.RoleService, Type: models.UserTypeService}, | ||
| }, | ||
| } | ||
|
|
||
| responses.NamespaceFromModel(namespace) | ||
|
|
||
| assert.Equal( | ||
| t, | ||
| []models.Member{ | ||
| {ID: "human", Email: "human@test.com", Role: authorizer.RoleOwner}, | ||
| {ID: "bot", Email: "bot@test.com", Role: authorizer.RoleService, Type: models.UserTypeService}, | ||
| }, | ||
| namespace.Members, | ||
| ) | ||
| } | ||
|
|
||
| func TestNamespaceFromModelProjectsEveryField(t *testing.T) { | ||
| createdAt := time.Date(2026, 9, 16, 12, 0, 0, 0, time.UTC) | ||
| settings := &models.NamespaceSettings{SessionRecord: true, SSHAccessMode: "identity"} | ||
| billing := &models.Billing{CustomerID: "cus_test"} | ||
|
|
||
| res := responses.NamespaceFromModel(&models.Namespace{ | ||
| Name: "namespace", | ||
| Owner: "000000000000000000000000", | ||
| TenantID: "00000000-0000-4000-0000-000000000000", | ||
| Members: []models.Member{{ID: "human", Email: "human@test.com", Role: authorizer.RoleOwner}}, | ||
| Settings: settings, | ||
| MaxDevices: 10, | ||
| CreatedAt: createdAt, | ||
| Billing: billing, | ||
| Type: models.TypeTeam, | ||
| DevicesAcceptedCount: 1, | ||
| DevicesPendingCount: 2, | ||
| DevicesRejectedCount: 3, | ||
| DevicesRemovedCount: 4, | ||
| }) | ||
|
|
||
| assert.Equal( | ||
| t, | ||
| &responses.Namespace{ | ||
| Name: "namespace", | ||
| Owner: "000000000000000000000000", | ||
| TenantID: "00000000-0000-4000-0000-000000000000", | ||
| Members: []responses.Member{{ID: "human", Email: "human@test.com", Role: authorizer.RoleOwner}}, | ||
| Settings: settings, | ||
| MaxDevices: 10, | ||
| CreatedAt: createdAt, | ||
| Billing: billing, | ||
| Type: models.TypeTeam, | ||
| DevicesAcceptedCount: 1, | ||
| DevicesPendingCount: 2, | ||
| DevicesRejectedCount: 3, | ||
| DevicesRemovedCount: 4, | ||
| }, | ||
| res, | ||
| ) | ||
| } | ||
|
|
||
| func TestNamespacesFromModel(t *testing.T) { | ||
| cases := []struct { | ||
| description string | ||
| namespaces []models.Namespace | ||
| expected []responses.Namespace | ||
| }{ | ||
| { | ||
| description: "projects an empty list when there are no namespaces", | ||
| namespaces: []models.Namespace{}, | ||
| expected: []responses.Namespace{}, | ||
| }, | ||
| { | ||
| description: "projects an empty list when the source is nil", | ||
| namespaces: nil, | ||
| expected: []responses.Namespace{}, | ||
| }, | ||
| { | ||
| description: "drops the service account from each namespace", | ||
| namespaces: []models.Namespace{ | ||
| { | ||
| Name: "first", | ||
| Members: []models.Member{ | ||
| {ID: "human", Email: "human@test.com", Role: authorizer.RoleOwner}, | ||
| {ID: "bot", Email: "bot@test.com", Role: authorizer.RoleService, Type: models.UserTypeService}, | ||
| }, | ||
| }, | ||
| { | ||
| Name: "second", | ||
| Members: []models.Member{{ID: "bot", Email: "bot@test.com", Role: authorizer.RoleService, Type: models.UserTypeService}}, | ||
| }, | ||
| }, | ||
| expected: []responses.Namespace{ | ||
| { | ||
| Name: "first", | ||
| Members: []responses.Member{{ID: "human", Email: "human@test.com", Role: authorizer.RoleOwner}}, | ||
| }, | ||
| { | ||
| Name: "second", | ||
| Members: []responses.Member{}, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| for _, tc := range cases { | ||
| t.Run(tc.description, func(t *testing.T) { | ||
| assert.Equal(t, tc.expected, responses.NamespacesFromModel(tc.namespaces)) | ||
| }) | ||
| } | ||
| } |
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.