Repository navigation
feat: [stable-8.0][OCSRE-1629] add NATS JWT authentication with a .creds file - #13220
Conversation
✅ Snyk checks have passed. No issues have been found so far.
💻 Catch issues earlier using the plugins for VS Code, JetBrains IDEs, Visual Studio, and Eclipse. |
|
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? |
54b0a13 to
efd1e50
Compare
efd1e50 to
f2ff4ad
Compare
|
Couple of general things:
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.
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
left a comment
There was a problem hiding this comment.
I guess this is review part 1 of ??? from me.
| // 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 } |
There was a problem hiding this comment.
Since this seems an error condition, should we just return nil to indicate the error instead of a "do nothing" function?
| return c.check | ||
| } | ||
|
|
||
| // NewNatsStoreCheck checks the reachability of the first node of a store. |
There was a problem hiding this comment.
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.
| 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() |
There was a problem hiding this comment.
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.
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. |
It is just a notification about the existing behavior. The new MICRO_REGISTRY_AUTH_CREDS_FILE and MICRO_REGISTRY_AUTH_CREDS follow this pattern. |
I don’t know, but this functionality is needed very urgently. |
mklos-kw
left a comment
There was a problem hiding this comment.
Automated code review summary — see inline comments for individual findings.
| 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"` |
There was a problem hiding this comment.
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...
a1d699c to
67b1feb
Compare
| 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"` |
There was a problem hiding this comment.
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
- 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]>
Signed-off-by: Julian Koberg <[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]>
7440ec6 to
a317dbf
Compare
…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]>
…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]>
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
OCIS_NATS_CREDS_FILE.credsfile (nats.UserCredentials). The client reads the file again on each reconnect, so a rotated JWT works without a restart.OCIS_NATS_CREDS.credsfile (nats.UserCredentialBytes). Use it when you cannot mount a file. It takes precedence overOCIS_NATS_CREDS_FILE.ocis.yamlfile.natsservice does not support JWT mode. Use an external NATS cluster andOCIS_EXCLUDE_RUN_SERVICES=nats.services/nats/README.mddescribes the steps.Changes
CredsFileandCredsfields in 17 events structs, 18 cache and store structs, and the shared cache.Credshas themask:"password"tag.store.Credentials(...)next to eachstore.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 newNewNatsCheckWithConfiguses the authentication and TLS settings of the service. Before this change, the NATS health checks connected without credentials and without TLS.nats.gov1.54.0,go 1.26.10.Dependencies
pkg/natsauth, events, stores).NatsOptionsoption for theevents/natsjsplugin.go.moduses a temporaryreplacetogithub.com/2403905/plugins. Do not merge before kobergj/plugins#3 and owncloud/reva#782 are merged andgo.modpoints to the final commits.How Has This Been Tested?
ocis-pkg/checks: health check against a NATS server in operator mode, with the creds file, the creds content, username and password, and invalid credsocis-pkg/natsjsregistry: the registry reads the global variables, and it checks the credentials only at startupservices/proxy/pkg/server/debug: the debug output masks every secret valuenats-serverin operator mode.ocis serverwithOCIS_EXCLUDE_RUN_SERVICES=nats,OCIS_CACHE_STORE=nats-js-kv, andOCIS_PERSISTENT_STORE=nats-js-kv.OCIS_NATS_CREDSand once withOCIS_NATS_CREDS_FILE.Types of changes
Checklist
services/nats/README.md)🤖 Generated with Claude Code