Skip to content
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

CBG-3720: Diagnostic API - get all user channels endpoint #6647

Merged
merged 26 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
6880b6c
Add get all user channels endpoint and TestGetAllChannelsByUser
mohammed-madi Jan 18, 2024
4136a20
Fix lint
mohammed-madi Jan 18, 2024
86454ff
Add licensing comments
mohammed-madi Jan 18, 2024
cc0e033
Fix test
mohammed-madi Jan 18, 2024
6ffe410
Fix test 2
mohammed-madi Jan 18, 2024
cb728fa
Add walrus check
mohammed-madi Jan 19, 2024
00a38f7
Fix goimports
mohammed-madi Jan 19, 2024
0935196
Add collections test and behaviour to endpoint
mohammed-madi Jan 22, 2024
b66431b
Use bodybytes instead of ResponseRecorder.Body.Bytes
mohammed-madi Jan 22, 2024
f83cc24
Return map[string]map[string]allChannels for collections
mohammed-madi Jan 22, 2024
4889f5c
Redisable TestGetAllChannelsByUser with walrus
mohammed-madi Jan 22, 2024
cefb1b4
Add docs
mohammed-madi Jan 22, 2024
6fba358
Fix docs path
mohammed-madi Jan 22, 2024
df40694
Fix extra leading underscore
mohammed-madi Jan 23, 2024
b45020c
Make and use marshalChannels for handleGetAllChannels
mohammed-madi Jan 23, 2024
583fa05
Fix comment
mohammed-madi Jan 23, 2024
a753f2e
Rollback to pre-source implementation and remove tests
mohammed-madi May 9, 2024
34e1726
Add most test cases, 404 and last 6 in spreadsheet left
mohammed-madi May 15, 2024
0c9be90
Add remaining test cases and remove unused code
mohammed-madi May 16, 2024
c6f1aeb
Fix error msg
mohammed-madi May 16, 2024
eb9953f
Fix docs
mohammed-madi May 16, 2024
dc66efe
Add comments and add missing doc file
mohammed-madi May 16, 2024
ad7402d
Add goimports
mohammed-madi May 16, 2024
2be2762
Address most comments except templateResource comment
mohammed-madi May 17, 2024
d102bc1
Template output for diagnostics to use for default collection or sing…
torcolvin May 21, 2024
c741176
Fix comments
mohammed-madi May 21, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions db/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -2438,3 +2438,19 @@ func (dbc *DatabaseContext) InstallPrincipals(ctx context.Context, spec map[stri
}
return nil
}

// DataStoreNames returns the names of all datastores connected to this database
func (db *Database) DataStoreNames() base.ScopeAndCollectionNames {
if db.Scopes == nil {
return base.ScopeAndCollectionNames{
base.DefaultScopeAndCollectionName(),
}
}
var names base.ScopeAndCollectionNames
for scopeName, scope := range db.Scopes {
for collectionName := range scope.Collections {
names = append(names, base.ScopeAndCollectionName{Scope: scopeName, Collection: collectionName})
}
}
return names
}
6 changes: 6 additions & 0 deletions docs/api/components/responses.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,9 @@ DB-config-precondition-failed:
example:
error: Precondition Failed
reason: Provided If-Match header does not match current config version
All_user_channels_response:
description: Map of all keyspaces to all channels that the user has access to.
content:
application/json:
schema:
$ref: ./schemas.yaml#/all_user_channels
25 changes: 25 additions & 0 deletions docs/api/components/schemas.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2512,3 +2512,28 @@ CollectionNames:
- Starting
- Stopping
- Resyncing
all_user_channels:
description: |-
All user channels split by how they were assigned to the user and by keyspace.
type: object
properties:
all_channels:
description: |-
All channels that the user has access to.
type: object
properties:
keyspace:
type: object
properties:
channel:
$ref: '#/channelEntry'
channelEntry:
description: Channel name
type: object
properties:
entries:
type: array
description: Start sequence to end sequence. If the channel is currently granted, the end sequence will be zero.
updated_at:
type: integer
description: Unix timestamp of last update
2 changes: 2 additions & 0 deletions docs/api/diagnostic.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ paths:
$ref: './paths/diagnostic/keyspace-sync.yaml'
'/{keyspace}/import_filter':
$ref: './paths/diagnostic/keyspace-import_filter.yaml'
'/{db}/_user/{name}/all_channels':
$ref: './paths/diagnostic/db-_user-name-_all_channels.yaml'
externalDocs:
description: Sync Gateway Quickstart | Couchbase Docs
url: 'https://docs.couchbase.com/sync-gateway/current/index.html'
28 changes: 28 additions & 0 deletions docs/api/paths/diagnostic/db-_user-name-_all_channels.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Copyright 2022-Present Couchbase, Inc.
#
# Use of this software is governed by the Business Source License included
# in the file licenses/BSL-Couchbase.txt. As of the Change Date specified
# in that file, in accordance with the Business Source License, use of this
# software will be governed by the Apache License, Version 2.0, included in
# the file licenses/APL2.txt.
parameters:
- $ref: ../../components/parameters.yaml#/db
- $ref: ../../components/parameters.yaml#/user-name
get:
summary: Get all channels for a user
description: |-
Retrieve all channels that a user has access to.

Required Sync Gateway RBAC roles:

* Sync Gateway Architect
* Sync Gateway Application
* Sync Gateway Application Read Only
responses:
'200':
$ref: ../../components/responses.yaml#/All_user_channels_response
'404':
$ref: ../../components/responses.yaml#/Not-found
tags:
- Database Security
operationId: get_db-_user-name_-all_channels
76 changes: 76 additions & 0 deletions rest/diagnostic_api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Copyright 2013-Present Couchbase, Inc.
//
// Use of this software is governed by the Business Source License included
// in the file licenses/BSL-Couchbase.txt. As of the Change Date specified
// in that file, in accordance with the Business Source License, use of this
// software will be governed by the Apache License, Version 2.0, included in
// the file licenses/APL2.txt.

package rest

import (
"fmt"

"github.com/couchbase/sync_gateway/auth"
"github.com/couchbase/sync_gateway/base"
channels "github.com/couchbase/sync_gateway/channels"
"github.com/gorilla/mux"
)

type allChannels struct {
Channels map[string]map[string]channelHistory `json:"all_channels,omitempty"`
}
type channelHistory struct {
Entries []auth.GrantHistorySequencePair `json:"entries"` // Entry for a specific grant period
}

func (h *handler) handleGetAllChannels() error {
h.assertAdminOnly()
user, err := h.db.Authenticator(h.ctx()).GetUser(internalUserName(mux.Vars(h.rq)["name"]))
mohammed-madi marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return fmt.Errorf("could not get user %s: %w", user.Name(), err)
}
if user == nil {
return kNotFoundError
}
mohammed-madi marked this conversation as resolved.
Show resolved Hide resolved

resp := make(map[string]map[string]channelHistory)

// handles deleted collections, default/ single named collection
for _, dsName := range h.db.DataStoreNames() {
keyspace := dsName.ScopeName() + "." + dsName.CollectionName()

currentChannels := user.InheritedCollectionChannels(dsName.ScopeName(), dsName.CollectionName())
chanHistory := user.CollectionChannelHistory(dsName.ScopeName(), dsName.CollectionName())
// If no channels aside from public and no channels in history, don't make a key for this keyspace
if len(currentChannels) == 1 && len(chanHistory) == 0 {
continue
}
resp[keyspace] = make(map[string]channelHistory)
for chanName, chanEntry := range currentChannels {
if chanName == channels.DocumentStarChannel {
continue
}
resp[keyspace][chanName] = channelHistory{Entries: []auth.GrantHistorySequencePair{{StartSeq: chanEntry.Sequence, EndSeq: 0}}}
}
for chanName, chanEntry := range chanHistory {
chanHistoryEntry := channelHistory{Entries: chanEntry.Entries}
// if channel is also in history, append current entry to history entries
if _, chanCurrentlyAssigned := resp[keyspace][chanName]; chanCurrentlyAssigned {
var newEntries []auth.GrantHistorySequencePair
newEntries = append(chanEntry.Entries, resp[keyspace][chanName].Entries...)
chanHistoryEntry.Entries = newEntries
}
resp[keyspace][chanName] = chanHistoryEntry
}

}
allChannels := allChannels{Channels: resp}

bytes, err := base.JSONMarshal(allChannels)
if err != nil {
return err
mohammed-madi marked this conversation as resolved.
Show resolved Hide resolved
}
h.writeRawJSON(bytes)
return err
}
Loading
Loading