Skip to content

feat: [stable-8.0][OCSRE-1629] add NATS JWT authentication with a .creds file - #13220

Merged
mklos-kw merged 38 commits into
stable-8.0from
OCSRE-1629-nats-creds
Nov 4, 2026
Merged

mklos-kw merged 38 commits into
stable-8.0from
OCSRE-1629-nats-creds

Conversation

@2403905

@2403905 2403905 commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

Description

Related pr owncloud/reva#782

A NATS server in operator (JWT) mode authenticates a client with a user JWT and an NKey seed. Before this change, oCIS could connect to NATS only with a username and a password. Thus oCIS could not use a shared NATS cluster with one NATS account for each instance.

This PR adds JWT authentication for all NATS connections: events, caches, persistent stores, the service registry, and the health checks.

New configuration

Variable Content
OCIS_NATS_CREDS_FILE The path of a NATS .creds file (nats.UserCredentials). The client reads the file again on each reconnect, so a rotated JWT works without a restart.
OCIS_NATS_CREDS The content of a .creds file (nats.UserCredentialBytes). Use it when you cannot mount a file. It takes precedence over OCIS_NATS_CREDS_FILE.
  • All services and the service registry use only these two global variables. There are no service-specific variables for the credentials.
  • The registry reads only environment variables, not the ocis.yaml file.
  • When JWT credentials are set, oCIS does not send the username and the password.
  • oCIS checks the credentials at startup. An invalid value gives a clear error.
  • The embedded nats service does not support JWT mode. Use an external NATS cluster and OCIS_EXCLUDE_RUN_SERVICES=nats. services/nats/README.md describes the steps.

Changes

  • Config: new CredsFile and Creds fields in 17 events structs, 18 cache and store structs, and the shared cache. Creds has the mask:"password" tag.
  • store.Credentials(...) next to each store.Authentication(...) call. The revaconfig maps pass the new reva keys.
  • ocis-pkg/natsjsregistry/registry.go: JWT credentials for the registry.
  • ocis-pkg/checks/checknats.go: the new NewNatsCheckWithConfig uses the authentication and TLS settings of the service. Before this change, the NATS health checks connected without credentials and without TLS.
  • Proxy health check: it used the events cluster ID as the NATS address. It now uses the events endpoint.
  • Proxy debug server: it now masks all secret configuration values. A new test checks this.
  • nats.go v1.54.0, go 1.26.10.

Dependencies

go.mod uses a temporary replace to github.com/2403905/plugins. Do not merge before kobergj/plugins#3 and owncloud/reva#782 are merged and go.mod points to the final commits.

How Has This Been Tested?

  • Unit tests:
    • ocis-pkg/checks: health check against a NATS server in operator mode, with the creds file, the creds content, username and password, and invalid creds
    • ocis-pkg/natsjsregistry: the registry reads the global variables, and it checks the credentials only at startup
    • services/proxy/pkg/server/debug: the debug output masks every secret value
  • reva and plugins: integration tests against a NATS server in operator mode with JetStream.
  • Manual test with the single binary:
    • An external nats-server in operator mode.
    • ocis server with OCIS_EXCLUDE_RUN_SERVICES=nats, OCIS_CACHE_STORE=nats-js-kv, and OCIS_PERSISTENT_STORE=nats-js-kv.
    • It ran once with OCIS_NATS_CREDS and once with OCIS_NATS_CREDS_FILE.
    • Result: no authorization errors, 10 KV buckets and the events stream in the JWT account, all events processed, and the readiness checks return 200. Upload and download work.

Types of changes

  • New feature (non-breaking change which adds functionality)
  • Bug fix (non-breaking change which fixes an issue)

Checklist

  • Code changes
  • Unit tests added
  • Documentation added (services/nats/README.md)
  • Changelog added

🤖 Generated with Claude Code

@kw-security

kw-security commented Oct 23, 2026 •

Copy link
Copy Markdown

✅ Snyk checks have passed. No issues have been found so far.

Status Scan Engine Critical High Medium Low Total (0)
✅ Open Source Security 0 0 0 0 0 issues
✅ Licenses 0 0 0 0 0 issues
✅ Code Security 0 0 0 0 0 issues

💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse.

@wkloucek

Copy link
Copy Markdown
Contributor

I gave it a test with this helmfile based testbed https://github.com/owncloud-koko/ocis-charts/pull/70 and it runs fine for me so far.

Is there anything specific I can test?

@2403905
2403905 requested a review from a team October 29, 2026 12:51
@2403905
2403905 marked this pull request as ready for review October 30, 2026 07:49
@2403905
2403905 requested a review from LukasHirt as a code owner October 30, 2026 07:49
@2403905
2403905 force-pushed the OCSRE-1629-nats-creds branch 2 times, most recently from 54b0a13 to efd1e50 Compare October 30, 2026 12:26
@2403905
2403905 requested a review from kobergj October 30, 2026 13:04
@2403905
2403905 force-pushed the OCSRE-1629-nats-creds branch from efd1e50 to f2ff4ad Compare October 30, 2026 13:12
@jvillafanez

Copy link
Copy Markdown
Member

Couple of general things:

The registry reads only environment variables, not the ocis.yaml file.

Any reason for this? I think it's pretty standard in oCIS to read from both places. I can understand security concerns about writing the credentials in the file, but I think it's the same as any other password already there, and it's the admin's decision to write the password in one place or another.

The embedded nats service does not support JWT mode. Use an external NATS cluster and OCIS_EXCLUDE_RUN_SERVICES=nats. services/nats/README.md describes the steps.

This will need documentation in the ownCloud docs. Even better if we add some reasoning (why can we set it up in the embedded nats? Would it be possible to configure the embedded nats as a cluster to support this feature?)

@jvillafanez jvillafanez left a comment

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.

I guess this is review part 1 of ??? from me.

Comment thread ocis-pkg/checks/checknats.go Outdated
// Thus each probe sends a PING to the server and waits for the PONG.
func NewNatsCheckWithConfig(address string, cfg stream.NatsConfig) func(context.Context) error {
if address == "" {
return func(context.Context) error { return nil }

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.

Since this seems an error condition, should we just return nil to indicate the error instead of a "do nothing" function?

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.

Good point.

Comment thread ocis-pkg/checks/checknats.go Outdated
return c.check
}

// NewNatsStoreCheck checks the reachability of the first node of a store.

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.

This will need documentation.
Side note: is this a limitation we have, or do we only expect one node? I'd expect to check for all the nodes, or at least until the first one that is down.

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.

fixed

Comment thread ocis-pkg/checks/checknats.go Outdated
Comment on lines +95 to +106
c.mu.Lock()
if c.conn != nil && !c.conn.IsClosed() {
conn := c.conn
c.mu.Unlock()
return conn, nil
}
if c.dial == nil {
c.dial = make(chan struct{})
go c.connect(c.dial)
}
dial := c.dial
c.mu.Unlock()

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.

I'd like to double check this because it seems too complex.
Rather than an async initialization (which is what this method seems to do), why not implement an async ping?
It's probably easier to use an existing connection (passed via parameter to this function), and assume the connection is ready (still need to check if the connection is closed or not). My point is that the connection should be initialized elsewhere.

@2403905

2403905 commented Oct 30, 2026

Copy link
Copy Markdown
Contributor Author

The embedded nats service does not support JWT mode. Use an external NATS cluster and OCIS_EXCLUDE_RUN_SERVICES=nats. services/nats/README.md describes the steps.

This will need documentation in the ownCloud docs. Even better if we add some reasoning (why can we set it up in the embedded nats? Would it be possible to configure the embedded nats as a cluster to support this feature?)

You are right we need to better document it. The embedded NATS is not the right place for JWT mode, for these reasons: JWT mode lets several oCIS instances share one NATS cluster. Each instance has its own account. The embedded server belongs to one instance, so there is nothing to separate. Settings: services/nats/pkg/server/nats/nats.go builds the server from Host, Port, ClusterID, StoreDir and the TLS settings. It has no operator, no system account and no account resolver.

@2403905

2403905 commented Oct 30, 2026

Copy link
Copy Markdown
Contributor Author

Couple of general things:

The registry reads only environment variables, not the ocis.yaml file.

Any reason for this? I think it's pretty standard in oCIS to read from both places. I can understand security concerns about writing the credentials in the file, but I think it's the same as any other password already there, and it's the admin's decision to write the password in one place or another.

It is just a notification about the existing behavior. The new MICRO_REGISTRY_AUTH_CREDS_FILE and MICRO_REGISTRY_AUTH_CREDS follow this pattern.

@2403905

2403905 commented Oct 30, 2026

Copy link
Copy Markdown
Contributor Author

I guess this is review part 1 of ??? from me.

I don’t know, but this functionality is needed very urgently.

@mklos-kw mklos-kw left a comment

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.

Automated code review summary — see inline comments for individual findings.

Comment thread ocis-pkg/natsjsregistry/registry.go Resolved
Comment thread services/gateway/pkg/config/nats.go Resolved
Comment thread ocis-pkg/config/envdecode/envdecode.go Resolved
Comment thread ocis-pkg/shared/shared_types.go Resolved
Comment thread ocis-pkg/natsjsregistry/registry.go Resolved
@2403905 2403905 changed the title feat: [stable-8.0][OCSRE-1629] add NATS JWT authentication with a .creds file feat: [stable-8.0][OCSRE-1629] add NATS JWT authentication with a .creds file [!] merge reva pr fist https://github.com/owncloud/reva/pull/782 Nov 1, 2026
AuthPassword string `mask:"password" yaml:"auth_password" env:"OCIS_CACHE_AUTH_PASSWORD" desc:"The password to use for authentication. Only applies when store type 'nats-js-kv' is configured." introductionVersion:"pre5.0"`
EnableTLS bool `yaml:"enable_tls" env:"OCIS_CACHE_ENABLE_TLS" desc:"Activate TLS for the connection to the NATS store. Only applies when store type 'nats-js-kv' is configured." introductionVersion:"8.3.0"`
TLSInsecure bool `yaml:"tls_insecure" env:"OCIS_CACHE_TLS_INSECURE" desc:"Disable TLS certificate verification for the NATS store connection. Only applies when store type 'nats-js-kv' is configured." introductionVersion:"8.3.0"`
TLSRootCACertificate string `yaml:"tls_root_ca_certificate" env:"OCIS_CACHE_TLS_ROOT_CA_CERTIFICATE" desc:"Path to the PEM-encoded root CA certificate used to validate the NATS store TLS certificate. Only applies when store type 'nats-js-kv' is configured." introductionVersion:"8.3.0"`

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We have here an introductionVersion of 8.3.0 which cant be.
This must be set to 9.0.0 because this envvar is introduced with this release. Just see this code in the ocs service to prove it.

Other envvars in this PR needs to be checked/fixed too.

Note that this also means, as this seems to be a backport, that all other open PRs or merges need to be fixed. If this is a backport, I am missing the reference to the sourcing PR for ease of finding...

@kobergj
kobergj force-pushed the OCSRE-1629-nats-creds branch from a1d699c to 67b1feb Compare November 3, 2026 13:37
Comment thread ocis-pkg/shared/shared_types.go Outdated
Comment on lines 83 to 85
CredsFile string `yaml:"creds_file" env:"OCIS_NATS_CREDS_FILE" desc:"Path to a NATS .creds file with a user JWT and an NKey seed. Use it to authenticate with the cache when NATS runs in JWT (operator) mode. When set, the service does not use the username and the password." introductionVersion:"8.0.10"`
Creds string `mask:"password" yaml:"creds" env:"OCIS_NATS_CREDS" desc:"Content of a NATS .creds file with a user JWT and an NKey seed. Use it to authenticate with the cache when NATS runs in JWT (operator) mode. The content takes precedence over the path of a .creds file. When set, the service does not use the username and the password." introductionVersion:"8.0.10"`
EnableTLS bool `yaml:"enable_tls" env:"OCIS_CACHE_ENABLE_TLS" desc:"Activate TLS for the connection to the NATS store. Only applies when store type 'nats-js-kv' is configured." introductionVersion:"8.3.0"`

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Maybe I made myself not clear enough in the past...

There is neither a introduction version 8.0.10 (we NEVER introduce an envvar with a patch release),
Nor a IV with 8.3.0 (does not exist, superceded by 9.0.0)

Both IVs (and all subsequent ones added MUST have 9.0.0

Thanks

mklos-kw
mklos-kw previously approved these changes Nov 4, 2026
mklos-kw
mklos-kw previously approved these changes Nov 4, 2026
@mklos-kw mklos-kw changed the title feat: [stable-8.0][OCSRE-1629] add NATS JWT authentication with a .creds file [!] merge reva pr fist https://github.com/owncloud/reva/pull/782 feat: [stable-8.0][OCSRE-1629] add NATS JWT authentication with a .creds file Nov 4, 2026
2403905 and others added 17 commits November 4, 2026 13:33
- Bump reva to 4358c1ee0. reva now has stream.NatsOptions and
  natsauth.Option.
- Set go.etcd.io/etcd/client/pkg/v3 back to v3.6.11. The earlier
  dependency bump set v3.7.3. That version does not compile with
  go.etcd.io/etcd/client/v3 v3.6.11.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Julian Koberg <[email protected]>
The NATS health check kept one connection and checked only its status.
The status stays CONNECTED until the client notices a lost server, so a
probe reported ready for minutes. The check also kept the lock during
the dial and ignored the probe context.

- Each probe sends a PING and waits for the PONG. The probe uses its
  context, or a limit of 2s when the context has no deadline.
- One dial runs without the lock. The other probes wait for it only
  until their context ends.
- An empty address means that the service does not use NATS. The
  check then succeeds. This fixes graph when its events are off.
- checks.NewNatsStoreCheck checks a store only for the nats-js and
  nats-js-kv types. The ocs and gateway checks use it.
- The health check uses reva stream.NatsOptions, so it has the same
  authentication and TLS rules as the event stream.

The username and the password now replace the JWT credentials only
when both have a value. Before, a leftover service password alone
removed the global credentials.

The registry logs a fatal error through the oCIS logger. It uses
natsauth.Option after the first check, so the option reads the
.creds file only when the connection connects.

The sharing and storage-users Events fields now have the names of
stream.NatsConfig. The NATS test helpers are in ocis-pkg/natstest.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Julian Koberg <[email protected]>
The userlog store was the only store without a default node. With
OCIS_PERSISTENT_STORE=nats-js-kv, the store could not connect and
userlog failed with "no servers available for connection" for each
event. The acceptance test run with NATS JWT credentials found this.
The problem does not depend on the credentials.

Use 127.0.0.1:9233 like the other services.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Signed-off-by: Julian Koberg <[email protected]>
Signed-off-by: Julian Koberg <[email protected]>
The scenario "check default services readiness (graph, idp, proxy)"
(serviceAvailabilityCheck.feature:111) passes now. The proxy health
check used the events cluster ID as the NATS address, so the proxy
readiness check failed. This pull request uses the events endpoint.

The test run reports a scenario that passes but is in the list of
expected failures as an error. Thus the job cliCommands,
apiServiceAvailability failed in each run of this pull request. The
only other failures in the job were the two expected failures at
lines 120 and 131.

Remove the entry for line 111. Keep the entries for the auth-bearer
scenarios at lines 120 and 131.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Signed-off-by: Julian Koberg <[email protected]>
The embedded nats service cannot run in JWT mode. Thus no job tested
the JWT credentials end to end. A client that connects without
credentials caused only retries in the background, so the tests could
pass.

Add the scripts in tests/nats-jwt:

- natscreds creates new keys and the NATS config at each run. The
  repository never has a seed.
- start-nats.sh starts a stand-alone NATS server in operator mode.
- verify.sh checks the NATS log for authentication errors, the KV
  buckets, the consumers of the events stream, and the isolation of
  the account.
- startup-check.sh starts oCIS with audit, policies, antivirus, and
  notifications, and requests /readyz of 17 services.

Add two jobs to the acceptance test workflow:

- nats-jwt-tests runs apiAsyncUpload, apiActivities, apiSearch1,
  apiSharingNgShares, apiSharingNgItemLinkShare, and apiNotification.
  The creds are a file or the content.
- nats-jwt-startup-check runs startup-check.sh.

Both jobs are in the needs of all-acceptance-tests.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Signed-off-by: Julian Koberg <[email protected]>
- WithCheck skips a nil check. The NATS checks return nil when nothing
  needs a check.
- The store check uses all nodes. It succeeds when one node is
  reachable, like the store connection.
- The check creates one connection with RetryOnFailedConnect. The nats
  client reconnects by itself. Each probe only sends a PING.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Signed-off-by: Julian Koberg <[email protected]>
Init built the store options two times. Init runs again on each NATS
reconnect, so the registry did the work two times in a reconnect storm.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Signed-off-by: Julian Koberg <[email protected]>
…ices

The envdecode package finds the NATS credentials of a struct by the
field names. It skips a struct without an error when a name does not
match. The new test walks the config of all services and fails when a
name does not match.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Signed-off-by: Julian Koberg <[email protected]>
owncloud/reva#782 is merged. Pin the merged commit instead of the commit
of the pull request branch. The vendored code does not change.

Co-Authored-By: Claude Opus 5 <[email protected]>
Signed-off-by: Julian Koberg <[email protected]>
…o 8.0.10

The NATS credentials variables are new in this pull request, which targets
stable-8.0, so they are introduced with the 8.0.10 release. The TLS variables
keep 8.3.0, because they are already released with 8.0.9.

Co-Authored-By: Claude Opus 5 <[email protected]>
Signed-off-by: Julian Koberg <[email protected]>
Signed-off-by: Michal Klos <[email protected]>
Signed-off-by: Julian Koberg <[email protected]>
…cope

The credsScope struct carries env tags, so check-env-var-annotations
requires a desc and an introductionVersion annotation on each field.
Without them the build-and-test job fails.

Co-Authored-By: Claude Opus 5 <[email protected]>
Signed-off-by: Julian Koberg <[email protected]>
…o 9.0.0

The introductionVersion of a new environment variable is always the latest
unreleased release, which is 9.0.0, also for a change that targets stable-8.0.

Co-Authored-By: Claude Opus 5 <[email protected]>
Signed-off-by: Julian Koberg <[email protected]>
The rebase resolved the go.sum conflict with golang.org/x/net v0.62.0 as a
union of both sides. go mod tidy removes the entries that are no longer
needed. go mod vendor reports no change.

Co-Authored-By: Claude Opus 5 <[email protected]>
Signed-off-by: Julian Koberg <[email protected]>
@kobergj
kobergj dismissed stale reviews from mklos-kw and mmattel via a317dbf November 4, 2026 12:39
@kobergj
kobergj force-pushed the OCSRE-1629-nats-creds branch from 7440ec6 to a317dbf Compare November 4, 2026 12:40
@mklos-kw
mklos-kw merged commit 99b5ec2 into stable-8.0 Nov 4, 2026
@mklos-kw
mklos-kw deleted the OCSRE-1629-nats-creds branch November 4, 2026 13:09
2403905 added a commit that referenced this pull request Nov 10, 2026
…file (#13382)

* feat: [master][OCSRE-1629] add NATS JWT authentication with a .creds file

Backport of #13220 to master.

The base of this commit is the backport to stable-9.0
(#13381). Changes for master:

- go.mod: use the reva backport owncloud/reva#799. The reva main
  branch needs newer versions of some modules, for example nats.go
  v1.55.0 and go 1.26.10. Use the plugins replace lines of the
  original change.
- vendor/: regenerate with go mod vendor.

(cherry picked from commit 99b5ec2)

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>

* build: [OCSRE-1629] pin the merged reva commit

owncloud/reva#799 is merged. Use the merged main commit instead of the
commit of the pull request branch. The vendored code does not change.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>

* ci: [OCSRE-1629] fix the zizmor findings in the NATS JWT jobs

- Set persist-credentials: false for the checkout steps.
- Use the action versions of master for setup-php and setup-node.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>

* ci: [OCSRE-1629] do not expect the cache-createhome bucket

The gateway does not use a NATS create-home cache anymore.
The proxy keeps this state in memory. Remove the bucket from
the list of buckets that verify.sh expects.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

---------

Co-authored-by: Claude Sonnet 5.5 <[email protected]>
2403905 added a commit that referenced this pull request Nov 10, 2026
…eds file (#13381)

* feat: [stable-9.0][OCSRE-1629] add NATS JWT authentication with a .creds file

Backport of #13220 to stable-9.0.

Changes for the backport:

- go.mod: use the reva backport owncloud/reva#798. Keep the NATS
  versions of stable-9.0. Use the plugins replace lines of the original
  change.
- services/gateway: stable-9.0 has no create home cache. Drop its
  credentials and its changelog text.
- services/storage-users/pkg/config/config.go: rename the Events fields
  to Endpoint, Cluster and TLSRootCACertificate, like the original
  change.
- .github/workflows/acceptance-tests.yml: add the jobs nats-jwt-tests
  and nats-jwt-startup-check to the workflow of stable-9.0. Use the
  action versions of stable-9.0.
- Keep the mask tags of the original change in the config structs.
- tests/acceptance/expected-failures-localAPI-on-OCIS-storage.md: no
  change. stable-9.0 has no entry for the scenario that the original
  change removed.

(cherry picked from commit 99b5ec2)

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>

* build: [OCSRE-1629] pin the reva backport with the plugins fix

Use the commit of owncloud/reva#798 that has the kobergj/plugins replace
lines. The vendored code does not change.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>

* build: [OCSRE-1629] pin the merged reva commit

owncloud/reva#798 is merged. Use the merged stable-9.0 commit instead of
the commit of the pull request branch.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>

* ci: [OCSRE-1629] set persist-credentials false in the NATS JWT jobs

Fix the zizmor findings for the checkout steps.

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>

* ci: [OCSRE-1629] do not expect the cache-createhome bucket

The gateway does not use a NATS create-home cache anymore.
The proxy keeps this state in memory. Remove the bucket from
the list of buckets that verify.sh expects.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

---------

Co-authored-by: Claude Sonnet 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants