Skip to content
This repository was archived by the owner on Sep 30, 2024. It is now read-only.

worker: add SAMS notifications subscriber#63051

Merged
unknwon merged 7 commits into
mainfrom
jc/sams-notifications
Jun 3, 2024
Merged

worker: add SAMS notifications subscriber#63051
unknwon merged 7 commits into
mainfrom
jc/sams-notifications

Conversation

@unknwon

@unknwon unknwon commented Jun 3, 2024

Copy link
Copy Markdown
Contributor

Part of CORE-92

This PR add a new worker for subscribing to SAMS notifications. The current use case is to automatically (hard-)delete users on Sourcegraph.com when the corresponding user is deleted from SAMS.

This worker is only started when running in the Sourcegraph.com mode and the credentials file (service_account.json) is provided, which has been configured since https://github.com/sourcegraph/deploy-sourcegraph-cloud/pull/18591.

Test plan

Tested locally end-to-end using my test GCP project.

  1. Create test service account and download the credentials file
  2. Create test Pub/Sub topic and subscription
  3. Assign "Pub/Sub Subscriber" role to the service account on the subscription
  4. Boot up SAMS locally
    export NOTIFICATION_GCP_PUBSUB_PROJECT="joes-playground-425319"
    export NOTIFICATION_GCP_PUBSUB_TOPIC="sams-notifications"
    overmind start
  5. Boot up SG in dotcom mode locally
    # sg.config.overwrite.yaml
    commandsets:
      dotcom:
        env:
          SOURCEGRAPHDOTCOM_MODE: true
          SOURCEGRAPH_ACCOUNTS_CREDENTIALS_FILE: './accounts-credentials.json'
          SOURCEGRAPH_ACCOUNTS_NOTIFICATIONS_PROJECT: 'joes-playground-425319'
    # dev-private/.../site-config.json
    "auth.providers": [
      {
        "allowSignup": true,
        "clientID": "sams_cid_018ea03c-55d2-765c-9074-98acfe0bc520",
        "clientSecret": "REDACTED",
        "displayName": "Sourcegraph Accounts",
        "configID": "sams",
        "issuer": "http://localhost:9991",
        "type": "openidconnect"
      }
    ]
    sg start dotcom
  6. Delete a SAMS user
    curl -X DELETE -H "Authorization: Bearer foolmeifyoucan"  http://localhost:9991/api/management/v1/users/018d21f2-10ba-7f83-84b9-615678d6b383
  7. Wait for SG to pick it up (temporarily changed from DEBUG to INFO logging)
    CleanShot 2024-06-03 at 15 43 14@2x

@cla-bot cla-bot Bot added the cla-signed label Jun 3, 2024
@unknwon unknwon force-pushed the jc/sams-notifications branch from 65cad0a to b163d1f Compare June 3, 2024 20:27
@unknwon unknwon marked this pull request as ready for review June 3, 2024 20:28
@unknwon unknwon requested a review from a team June 3, 2024 20:28
@unknwon unknwon force-pushed the jc/sams-notifications branch from b163d1f to a014c8b Compare June 3, 2024 20:31
Comment thread cmd/worker/internal/sourcegraphaccounts/notifications_subscriber.go Outdated
Comment thread cmd/worker/internal/sourcegraphaccounts/notifications_subscriber.go Outdated
Comment thread cmd/worker/internal/sourcegraphaccounts/notifications_subscriber.go Outdated
Comment thread cmd/worker/internal/sourcegraphaccounts/notifications_subscriber.go
Comment on lines +104 to +111
credentialsJSON, err := os.ReadFile(s.config.GCP.CredentialsFile)
if err != nil {
return nil, errors.Wrap(err, "read GCP credentials file")
}
credentials, err := google.CredentialsFromJSON(ctx, credentialsJSON, pubsub.ScopePubSub)
if err != nil {
return nil, errors.Wrap(err, "parse GCP credentials JSON")
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this happen up-front in config load instead?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think all of config init steps here equally crucial, and CredentialsFromJSON doesn't really do any validation other than expecting valid JSON format. Thus prefer init them in the same order as the fields of notificationsv1.SubscriberOptions.

c.GCP.SubscriptionID = c.Get("SOURCEGRAPH_ACCOUNTS_NOTIFICATIONS_SUBSCRIPTION", "sams-notifications", "GCP Pub/Sub subscription ID to receive SAMS notifications from")
}

func handleOnUserDeleted(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO the handlers deserve their own file for readability and expansion - e.g. in the future, we might have:

user_deletion.go
user_rename.go

etc.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I ended up restructuring the files a bit in https://github.com/sourcegraph/sourcegraph/pull/63051/commits/f7ab03a3a8ebcad6e69670bf6e95cfd23ddf87c8, PTAL!

I do want anything notifications-related visually and structurally grouped, since this package is generic "sourcegraphaccounts", who knows if we gonna add more workers here 😁

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Much nicer, thanks 😁

Comment thread mockgen.test.yaml
Comment on lines +350 to +353
- filename: cmd/worker/internal/sourcegraphaccounts/mocks_test.go
path: github.com/sourcegraph/sourcegraph/cmd/worker/internal/sourcegraphaccounts
interfaces:
- notificationsSubscriberStore

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:yes:

@unknwon unknwon merged commit dd8ff60 into main Jun 3, 2024
@unknwon unknwon deleted the jc/sams-notifications branch June 3, 2024 22:01
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants