bindan 0.1.2 → 0.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b9be963a07d9adaf6952ec6a57c47ee853e20e601608e5e8d15bdbd9b23ab8b9
4
- data.tar.gz: 4a23535e819cee5eea21bf664df8c25ab4ea7dc4fa9714e814a2b9e0153d4bb7
3
+ metadata.gz: d8d6bd2055debe3414cc3c1c82802dd55aae38da0e493f8ce0252083bf62cc5f
4
+ data.tar.gz: c6de5a2b13a1aeda060bd5886f973c21293d87cace68641a84119e51ae81e20f
5
5
  SHA512:
6
- metadata.gz: 41ff4d4d2d7c3467ee888a5c4b116ac47e7b2bb19764e2026b7d8c797f10fed4c7470cacfd663771c47dc5cfff28602427080872434f2bd269e80c3d7f4952f4
7
- data.tar.gz: 271b943d0837381c9c11dd9169771116c47cdda6caad9c0d51c7d91e2c95c1a4d06e51a97e53e3adfd1d1cc1f452949054511caa1ed34415b36404587a1eac6e
6
+ metadata.gz: a1e7b8c963cec2bbf37f6324c4a0349c0e890420d7262296427d34e6ba263dc775b1ec87ddbbb2fc5958f287231beb9dbaf77f76afd33287669f2e5ba9973b91
7
+ data.tar.gz: 676a894f98eb6a3e2a29a0a5e071064b8befb1071e50f29d61792a4073fea542618f22bd0c30d37797420e3820b484317696809d94dff9b911ea6faeda9a45c4
data/CHANGELOG.md CHANGED
@@ -1,4 +1,13 @@
1
- ## [Unreleased]
1
+ ## [0.2.1] - 2026-11-15
2
+
3
+ - (feat) Add `Bindan::SecretManagerHelper` and `bindan-secret-update` to add Secret Manager versions from exact file bytes or a known-good numerical version. The helper also retrieves the last created version regardless of its state.
4
+ - (refactor) Make Firestore and Secret Manager emulator controller errors inherit from `Bindan::Error`.
5
+
6
+ ## [0.2.0] - 2026-11-13
7
+
8
+ ### Added
9
+
10
+ - Add `Bindan::Provider::SecretManager` to read configuration from Google Cloud Secret Manager.
2
11
 
3
12
  ## [0.1.2] - 2026-11-12
4
13
 
data/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
  [![Gem Version](https://badge.fury.io/rb/bindan.svg)](https://badge.fury.io/rb/bindan)
3
3
  [![CI](https://github.com/wtnabe/bindan/actions/workflows/test.yml/badge.svg)](https://github.com/wtnabe/bindan/actions/workflows/test.yml)
4
4
 
5
- Bindan is a Ruby gem for building single configuration object from various sources with providers (that is bundled Google Cloud Storage and Firestore platform by default). It provides a flexible way to manage application settings, supporting lazy initialization and seamless switching between development (using emulators) and production (eg. actual GCP services) environments.
5
+ Bindan is a Ruby gem for building a single configuration object from various sources with providers (including bundled Google Cloud Storage, Firestore, and Secret Manager providers). It provides a flexible way to manage application settings, supporting lazy initialization and seamless switching between development (using emulators) and production (eg. actual GCP services) environments.
6
6
 
7
7
  ## Features
8
8
 
@@ -11,6 +11,7 @@ Bindan is a Ruby gem for building single configuration object from various sourc
11
11
  - Environment Variables
12
12
  - Google Cloud Storage
13
13
  - Google Cloud Firestore
14
+ - Google Cloud Secret Manager
14
15
  - your custom provider
15
16
 
16
17
  ## Installation
@@ -76,6 +77,7 @@ in Gemfile
76
77
  gem "bindan"
77
78
  gem "google-cloud-storage"
78
79
  gem "google-cloud-firestore"
80
+ gem "google-cloud-secret_manager"
79
81
  ```
80
82
 
81
83
  and
@@ -84,12 +86,14 @@ and
84
86
  require "bindan"
85
87
  require "google/cloud/storage"
86
88
  require "google/cloud/firestore"
89
+ require "google/cloud/secret_manager"
87
90
 
88
91
  # Define your configuration providers
89
92
  providers = {
90
93
  env: Bindan::Provider::Envvar.new,
91
94
  storage: Bindan::Provider::Storage.new("config-bucket", project_id: "my-project"),
92
- firestore: Bindan::Provider::Firestore.new("app-settings-collection", project_id: "my-project")
95
+ firestore: Bindan::Provider::Firestore.new("app-settings-collection", project_id: "my-project"),
96
+ secret: Bindan::Provider::SecretManager.new(project_id: "my-project")
93
97
  }
94
98
 
95
99
  # Configure Bindan
@@ -98,18 +102,54 @@ config = Bindan.configure(providers: providers) do |c, pr|
98
102
  c.database_host = pr.env["DATABASE_HOST"] || "localhost"
99
103
  c.api_key = pr.storage["api_key"]
100
104
  c.feature_flags = pr.firestore["feature_flags_document"]
105
+ c.api_token = pr.secret["api-token"]
101
106
  end
102
107
 
103
108
  # Access your configuration values
104
109
  puts config.database_host # => "localhost" (or value from ENV["DATABASE_HOST"])
105
110
  puts config.api_key # => (Value loaded from Google Cloud Storage)
106
111
  puts config.feature_flags # => (Value loaded from Firestore)
112
+ # Use config.api_token without printing or logging it.
107
113
  ```
108
114
 
109
115
  In the `configure` block, you define how each configuration key is mapped to a provider. The provider name is `providers` Hash key.
110
116
 
111
117
  Replace `my-project` with your Google Cloud project ID. Storage joins `project_id` and the bucket argument with `separator` (default: `"-"`), so this example reads the `api_key` object from the existing `my-project-config-bucket` bucket. Firestore reads the `app-settings-collection/feature_flags_document` document in the same project.
112
118
 
119
+ Secret Manager also accepts an omitted `project_id:`, using `GOOGLE_CLOUD_PROJECT`
120
+ or `GCLOUD_PROJECT` from the environment. An explicit `project_id:` takes precedence.
121
+
122
+ ### Reflecting configuration changes
123
+
124
+ The current Bindan implementation retains the values resolved by `configure`;
125
+ `config` does not reread the Providers. For now, reflect source changes by replacing
126
+ the application's execution units to acquire the updated values. See the
127
+ [configuration rollout guide](docs/configuration-rollout.md) for App Engine and Cloud Run.
128
+
129
+ ### Updating and reverting Secret Manager values
130
+
131
+ If you need to revert a Secret Manager value, take care: GCP and the emulator
132
+ differ in how `latest` handles disabled versions. `bindan-secret-update` supports
133
+ both updates and reverts by adding a new version without exposing
134
+ the value.
135
+
136
+ Install `google-cloud-secret_manager` alongside `bindan`. Use a file for an update,
137
+ or a known-good numerical version for a revert:
138
+
139
+ ```bash
140
+ bundle exec bindan-secret-update --project my-project --secret api-key \
141
+ --file ./api-key.bin
142
+
143
+ bundle exec bindan-secret-update --project my-project --secret api-key \
144
+ --from-version 1
145
+ ```
146
+
147
+ For the behavior differences and the full workflow, see
148
+ [Secret Manager operations from Cloud Shell](docs/secret-manager-operations.md)
149
+ before performing a revert. Neither command refreshes running applications.
150
+ For direct Ruby use, see the example in
151
+ [`Bindan::SecretManagerHelper`](lib/bindan/secret_manager_helper.rb).
152
+
113
153
  ## Development
114
154
 
115
155
  After checking out the repo, run `bin/setup` to install development dependencies and pull the emulator images. It does not start the containers.
@@ -125,7 +165,7 @@ You can also run `bin/console` for an interactive prompt that will allow you to
125
165
  The development environment includes the following tools for running heavy integration tests locally:
126
166
 
127
167
  1. **Docker with Docker Compose:** [Get Docker](https://docs.docker.com/get-docker/), with its daemon running.
128
- 2. **Emulators:** `compose.yml` defines fake-gcs-server and the official Google Cloud CLI emulator image. Java and Firestore are provided inside the image; no host Java or Google Cloud CLI installation is required. Storage fixtures are mounted directly from `spec/support/storage`.
168
+ 2. **Emulators:** `compose.yml` defines fake-gcs-server, the official Google Cloud CLI Firestore emulator image, and the unofficial Secret Manager emulator. Java and Firestore are provided inside the image; no host Java or Google Cloud CLI installation is required. Storage fixtures are mounted directly from `spec/support/storage`.
129
169
 
130
170
  ### Custom Provider
131
171
 
@@ -162,11 +202,15 @@ docker compose down
162
202
 
163
203
  CI runs both suites and manages Compose startup, logs and cleanup in the workflow, including cleanup after failures.
164
204
 
165
- Integration tests run serially against Storage on `127.0.0.1:4443` and Firestore on
166
- `127.0.0.1:8080`. Each Firestore case clears the `bindan-test` project's default
167
- database before seeding data. Use these emulators for disposable test data only.
168
- The Firestore controller waits for an SDK request to succeed, not merely a TCP
169
- connection. The suite does not import or export Firestore data.
205
+ Integration tests run serially against Storage on `127.0.0.1:4443`, Firestore on
206
+ `127.0.0.1:8080`, and Secret Manager on `127.0.0.1:9090`. Each Firestore case clears
207
+ the `bindan-test` project's default database before seeding data. Secret Manager
208
+ cases clear secrets in the `bindan-test` project before and after each case.
209
+ Use these emulators for disposable, non-sensitive test data only.
210
+ The Firestore and Secret Manager controllers wait for an SDK request to succeed,
211
+ not merely a TCP connection. The suite does not import or export emulator data.
212
+ To use dummy Storage credentials without local GCP credentials, run
213
+ `CI=true bundle exec rake spec:integration`.
170
214
 
171
215
  To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
172
216
 
data/compose.yml CHANGED
@@ -24,3 +24,13 @@ services:
24
24
  - --edition=standard
25
25
  ports:
26
26
  - "127.0.0.1:8080:8080"
27
+ secret-manager:
28
+ image: ghcr.io/blackwell-systems/gcp-secret-manager-emulator-dual:1.9.0
29
+ environment:
30
+ IAM_MODE: "off"
31
+ GCP_MOCK_LOG_LEVEL: error
32
+ GCP_MOCK_PERSIST: "false"
33
+ tmpfs:
34
+ - /data
35
+ ports:
36
+ - "127.0.0.1:9090:9090"
@@ -0,0 +1,162 @@
1
+ # Reflecting configuration changes in running applications
2
+
3
+ ## Current limitation and rollout approach
4
+
5
+ The current Bindan implementation evaluates the configuration block when
6
+ `configure` runs and retains the resolved values. Subsequent `config` calls return
7
+ that configuration without reading the Providers again. Changing a value at its source does not change
8
+ the configuration already held by a running application.
9
+
10
+ Given this current limitation, this guide reflects changes by completely replacing
11
+ the execution units that hold the old configuration, such as instances or containers.
12
+ The new units run the application's configuration setup and acquire the updated values. Changing
13
+ traffic routing alone does not cause an existing unit to reread configuration.
14
+
15
+ ## Rollout sequence
16
+
17
+ 1. Update the configuration source and verify the values the application will
18
+ read. Follow that source's update and consistency requirements before proceeding;
19
+ source-specific operations are outside this guide.
20
+ 2. Replace every execution unit that can hold the old configuration, keeping
21
+ enough capacity available to serve requests.
22
+ 3. Verify that the new units work with the updated configuration and that old
23
+ units no longer handle requests.
24
+
25
+ New and old configurations can coexist from the moment the source changes:
26
+ the platform may start new units independently of this procedure. Keep the
27
+ application compatible with both configurations during the transition. Coordinate
28
+ the rollout with other configuration changes and application deployments.
29
+
30
+ ## Replace execution units
31
+
32
+ The examples use Cloud Shell and gcloud. Authorize the session when prompted and
33
+ check the account and target project. Instance replacement and revision deployment
34
+ require the hosting service's permissions, separately from source read/write access.
35
+ These operations affect capacity, running requests, and potentially cost. Start
36
+ in a non-production environment and use the application's normal health checks.
37
+
38
+ ```bash
39
+ PROJECT_ID="my-project"
40
+ ```
41
+
42
+ For external-source changes, reuse the deployed application code or image rather
43
+ than rebuilding it. For deployment-supplied values such as environment variables,
44
+ update the deployment definition first; replacing units with the unchanged
45
+ definition would supply the same values. Choose the hosting service below.
46
+
47
+ ### App Engine Standard
48
+
49
+ After the source is updated and ready to read, record the UTC cutoff time. Units
50
+ started before this point may retain the old configuration and must be replaced:
51
+
52
+ ```bash
53
+ REPLACE_BEFORE="$(date -u +%Y-%m-%dT%H:%M:%SZ)"
54
+ ```
55
+
56
+ For each service and version that can receive requests, list instances and
57
+ review their `id` and `startTime` against `REPLACE_BEFORE`:
58
+
59
+ ```bash
60
+ APP_SERVICE="default"
61
+ APP_VERSION="my-version"
62
+ gcloud app instances list --project "$PROJECT_ID" \
63
+ --service "$APP_SERVICE" --version "$APP_VERSION" --format=json
64
+ ```
65
+
66
+ Stop instances started before the cutoff **one at a time**, checking replacement
67
+ capacity and application health between operations. App Engine starts instances
68
+ according to its scaling configuration and demand; stopping one does not promise
69
+ an immediately running replacement. Do not delete all instances at once.
70
+
71
+ ```bash
72
+ INSTANCE_ID="replace-with-reviewed-instance-id"
73
+ gcloud app instances delete "$INSTANCE_ID" --project "$PROJECT_ID" \
74
+ --service "$APP_SERVICE" --version "$APP_VERSION"
75
+ ```
76
+
77
+ List again until no pre-cutoff instances remain for all relevant services and
78
+ versions. Include versions reachable through direct version URLs, not only the
79
+ default traffic allocation. Check errors and successful use of the updated
80
+ configuration without logging sensitive values. Instance age identifies the
81
+ replacement targets; it does not prove which values the application loaded.
82
+
83
+ See [instance deletion](https://cloud.google.com/sdk/gcloud/reference/app/instances/delete)
84
+ and [instance fields](https://cloud.google.com/appengine/docs/admin-api/reference/rest/v1/apps.services.versions.instances#Instance).
85
+
86
+ ### Cloud Run
87
+
88
+ Create a new revision of the same image with a changed, non-sensitive generation
89
+ marker. The marker only forces a fresh revision; Bindan does not interpret it.
90
+ First review the current traffic, serving revision, and its image digest:
91
+
92
+ ```bash
93
+ RUN_SERVICE="my-service"
94
+ REGION="asia-northeast1"
95
+ gcloud run services describe "$RUN_SERVICE" --project "$PROJECT_ID" \
96
+ --region "$REGION" --format='yaml(status.traffic)'
97
+ SERVING_REVISION="replace-with-reviewed-serving-revision"
98
+ gcloud run revisions describe "$SERVING_REVISION" --project "$PROJECT_ID" \
99
+ --region "$REGION" --format='value(status.imageDigest)'
100
+ ```
101
+
102
+ The example below assumes a single-container service, one serving revision, and
103
+ a service template matching that revision's intended configuration. If there are
104
+ pending configuration changes, sidecars, or an existing traffic split, adapt the
105
+ normal deployment procedure instead of overwriting it with this example. Do not
106
+ run concurrently with other deployments. Use the reviewed full image reference
107
+ with `@sha256:...`, not a mutable tag, and keep all other settings unchanged.
108
+
109
+ ```bash
110
+ IMAGE="replace-with-current-image-reference@sha256:..."
111
+ GENERATION="cfg-$(date -u +%Y%m%d%H%M%S)"
112
+ NEW_REVISION="$RUN_SERVICE-$GENERATION"
113
+ gcloud run deploy "$RUN_SERVICE" --project "$PROJECT_ID" --region "$REGION" \
114
+ --image "$IMAGE" --revision-suffix "$GENERATION" \
115
+ --update-env-vars "BINDAN_CONFIG_GENERATION=$GENERATION" \
116
+ --no-traffic --tag "$GENERATION"
117
+ ```
118
+
119
+ Choose a unique generation that keeps the full revision name within Cloud Run's
120
+ length limit. Verify the deployment succeeded and test the new revision through
121
+ its tagged URL using the service's normal authentication. Check that the
122
+ application actually works with the new configuration, not just that it starts.
123
+ Then move a small proportion of traffic and monitor it before completing:
124
+
125
+ ```bash
126
+ gcloud run services update-traffic "$RUN_SERVICE" \
127
+ --project "$PROJECT_ID" --region "$REGION" \
128
+ --to-revisions "$SERVING_REVISION=95,$NEW_REVISION=5"
129
+ # After verifying health, move the remaining traffic.
130
+ gcloud run services update-traffic "$RUN_SERVICE" \
131
+ --project "$PROJECT_ID" --region "$REGION" --to-revisions "$NEW_REVISION=100"
132
+ ```
133
+
134
+ Confirm the resulting traffic allocation and allow in-flight requests to finish.
135
+ Review old tagged URLs and minimum-instance settings too: zero main-service
136
+ traffic does not mean an old revision cannot still run or receive direct requests.
137
+ See [Cloud Run traffic migration and required roles](https://cloud.google.com/run/docs/rollouts-rollbacks-traffic-migration).
138
+
139
+ ## Verify completion
140
+
141
+ Confirm that all request-serving execution units use the updated configuration,
142
+ including units reachable through direct version or tagged URLs. Monitor errors
143
+ and normal application operations that depend on the changed settings; a successful
144
+ restart or deployment alone is not sufficient. Do not log sensitive configuration
145
+ values while checking them.
146
+
147
+ If a new unit loads an unexpected value, check the source and the application's
148
+ read path before replacing more units. Correct the cause and repeat the replacement
149
+ as needed. Retire old values only after confirming that no request-serving units
150
+ still depend on them.
151
+
152
+ ## Revert if needed
153
+
154
+ Restore the known-good values using the source's rollback procedure, verify what
155
+ the application will read, and repeat the execution-unit replacement and completion
156
+ checks. Restoring the source alone does not change values already held by Bindan.
157
+
158
+ For Cloud Run, create another fresh revision with a new generation marker. Merely
159
+ sending traffic to an old revision does not reliably restore externally loaded
160
+ configuration: retained instances may have cached values and newly started ones
161
+ read the current source. Verify application behavior and replacement completion,
162
+ not only that the source update or deployment command succeeded.
@@ -0,0 +1,126 @@
1
+ # Secret Manager operations from Cloud Shell
2
+
3
+ ## Update and revert
4
+
5
+ Bindan's Secret Manager Provider reads `latest`. The update sequence is:
6
+
7
+ 1. Add a new version with the CLI and record the returned version name.
8
+ 2. Inspect the last created version without filtering by state or resolving an alias.
9
+ 3. Follow the [configuration rollout guide](configuration-rollout.md) to replace
10
+ the execution units holding Bindan's previous configuration.
11
+
12
+ For credentials, enable the new credential first and revoke the old one only
13
+ after verifying the application rollout. Serialize updates to a secret and
14
+ coordinate them with application deployments.
15
+
16
+ Updating multiple secrets is not a single transaction. If an update fails partway
17
+ through, earlier updates remain in effect; neither the CLI nor the Helper rolls
18
+ them back automatically. An application starting during the updates can also
19
+ load a mixture of old and new values. Plan for these combinations and partial
20
+ failures, recording the known-good version of each secret before making changes.
21
+
22
+ Revert also means adding a version: copy a known-good numerical version into a
23
+ new one, then repeat version checks and application rollout. Disabling or
24
+ destroying the newest version does **not** revert it. GCP's `latest` selects the
25
+ newest created version and fails if it is disabled or destroyed; the emulator
26
+ may return an older enabled version instead.
27
+
28
+ The CLI does not replace instances, move traffic, or guarantee convergence of
29
+ `latest`. Start with a non-sensitive secret in a non-production environment.
30
+
31
+ ## 1. Prepare Cloud Shell
32
+
33
+ Cloud Shell includes Ruby and gcloud, not Bindan or the Secret Manager Ruby SDK.
34
+ Include `bindan` and `google-cloud-secret_manager` in your Gemfile, then install
35
+ the dependencies:
36
+
37
+ ```bash
38
+ bundle install
39
+ bundle exec bindan-secret-update --help
40
+ ```
41
+
42
+ Authorize the session when Cloud Shell prompts. Normally no
43
+ `gcloud auth application-default login` is needed in Cloud Shell; the Ruby SDK
44
+ uses [Application Default Credentials](https://cloud.google.com/docs/authentication/set-up-adc-local-dev-environment#local-user-cred).
45
+ Check for credential overrides such as `GOOGLE_APPLICATION_CREDENTIALS` or a local
46
+ ADC file: the SDK and gcloud can otherwise use different identities. Do not print
47
+ tokens or credential files to diagnose access problems.
48
+
49
+ Updating requires `secretmanager.versions.add`; reverting also requires
50
+ `secretmanager.versions.access` to read the source. Inspecting the last created
51
+ version requires `secretmanager.versions.list`. The application's identity can
52
+ remain read-only for secrets.
53
+
54
+ ```bash
55
+ set +x
56
+ umask 077
57
+ export CLOUDSDK_CORE_LOG_HTTP=false CLOUDSDK_CORE_VERBOSITY=error
58
+ PROJECT_ID="my-project"
59
+ SECRET_ID="api-key"
60
+ gcloud auth list --filter=status:ACTIVE --format='value(account)'
61
+ gcloud config get-value project
62
+ ```
63
+
64
+ Use explicit project flags below to avoid depending on gcloud's active project.
65
+ Without `--project`, Bindan uses `GOOGLE_CLOUD_PROJECT`, then `GCLOUD_PROJECT`.
66
+ Cloud Shell initially sets `GOOGLE_CLOUD_PROJECT` from the console's active
67
+ project, but changing gcloud's configuration does not set Bindan's environment.
68
+
69
+ Prepare a protected file outside the checkout containing the exact new bytes.
70
+ Do not embed the value in a shell command, history, source control, or build
71
+ configuration. Avoid `echo`, which can change newlines. Cloud Shell's home
72
+ directory persists between sessions; remove plaintext files when no longer
73
+ needed, or use an appropriate ephemeral location. Removal is not secure erasure.
74
+
75
+ ## 2. Update or revert
76
+
77
+ Record the known-good **numerical** version before making a change. For an update:
78
+
79
+ ```bash
80
+ created="$(bundle exec bindan-secret-update \
81
+ --project "$PROJECT_ID" --secret "$SECRET_ID" --file /path/to/api-key.bin)"
82
+ ```
83
+
84
+ For a revert, use the same command with a source version instead of a file:
85
+
86
+ ```bash
87
+ SOURCE_VERSION="1" # Replace with the recorded known-good version.
88
+ created="$(bundle exec bindan-secret-update \
89
+ --project "$PROJECT_ID" --secret "$SECRET_ID" --from-version "$SOURCE_VERSION")"
90
+ ```
91
+
92
+ Run only the chosen form and check its exit status before continuing. Both
93
+ preserve the input bytes, suppress SDK payload logging, and print only the
94
+ created version name on success. Exit status is `0` for success, `2` for argument
95
+ errors, and `1` for operational failures. Writes are not automatically retried.
96
+ **A failed command may still have created a version.** Check the target and
97
+ whether the intended value was added before retrying; do not blindly rerun it.
98
+
99
+ ## 3. Inspect the last created version
100
+
101
+ The CLI succeeds when the SDK returns the newly created version. It does not
102
+ read back and compare secret contents. To inspect the last created version,
103
+ list all versions in creation order without filtering by state:
104
+
105
+ ```bash
106
+ gcloud secrets versions list "$SECRET_ID" --project "$PROJECT_ID" \
107
+ --sort-by='~createTime' --limit=1 --format='value(name)'
108
+ ```
109
+
110
+ Compare this name with the recorded `created` name. Disabled and destroyed versions
111
+ are included: the last created version is not necessarily an enabled or known-good
112
+ version. Do not use `latest` as a substitute for this lookup, because its behavior
113
+ differs between GCP and the emulator when versions are disabled or destroyed.
114
+
115
+ Listing is [eventually consistent](https://cloud.google.com/secret-manager/docs/access-secret-version#resource_consistency).
116
+ If the new version is not visible yet, wait and repeat the lookup, not the update.
117
+ The update's result identifies the version it created even while listing lags.
118
+ A match does not guarantee that all application reads have converged; allow for
119
+ propagation and check application health during rollout.
120
+
121
+ ## Next: reflect the value into the application
122
+
123
+ Continue with [Reflecting configuration changes in running applications](configuration-rollout.md)
124
+ to replace execution units and let Bindan acquire the updated values.
125
+ Keep the `latest` lookup unchanged; do not pin a numerical version in the application
126
+ to make this change. Follow the same version checks and rollout steps after a revert.
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bindan/secret_update_command"
5
+
6
+ exit Bindan::SecretUpdateCommand.new.run(ARGV)
@@ -13,7 +13,7 @@ module Bindan
13
13
  INITIAL_BACKOFF_KEY_SEC = 0.1 # seconds
14
14
  BACKOFF_MULTIPLIER = 1.5
15
15
 
16
- class Error < StandardError; end
16
+ class Error < Bindan::Error; end
17
17
 
18
18
  attr_reader :project_id
19
19
 
@@ -0,0 +1,105 @@
1
+ require "google/cloud/secret_manager/v1"
2
+ require "grpc"
3
+ require "logger"
4
+ require_relative "../error"
5
+
6
+ module Bindan
7
+ module Emulator
8
+ #
9
+ # Checks readiness and clears local test data without managing containers.
10
+ #
11
+ class SecretManagerController
12
+ WAIT_TIMEOUT = 60 # seconds
13
+
14
+ class Error < Bindan::Error; end
15
+
16
+ attr_reader :project_id, :client
17
+
18
+ #
19
+ # Connect to the local emulator without authentication or payload logging.
20
+ #
21
+ # @param [String] project_id project whose test secrets may be deleted
22
+ #
23
+ def initialize(project_id: "bindan-test")
24
+ @project_id = project_id
25
+ @channel = GRPC::Core::Channel.new(endpoint, {}, :this_channel_is_insecure)
26
+ # @type var retry_codes: Array[Integer]
27
+ retry_codes = []
28
+ @client = Google::Cloud::SecretManager::V1::SecretManagerService::Client.new do |config|
29
+ config.endpoint = endpoint
30
+ config.credentials = @channel
31
+ config.logger = Logger.new(nil)
32
+ config.timeout = 2
33
+ config.retry_policy = {retry_codes: retry_codes}
34
+ end
35
+ end
36
+
37
+ #
38
+ # @return [String] loopback SDK endpoint (host:port, without an HTTP scheme)
39
+ #
40
+ def endpoint
41
+ "127.0.0.1:9090"
42
+ end
43
+
44
+ #
45
+ # Wait for an SDK list request to succeed, retrying transient failures.
46
+ #
47
+ # @param [Numeric] timeout readiness deadline in seconds
48
+ # @return [self] after a successful SDK read
49
+ # @raise [Error] when the readiness deadline expires between attempts
50
+ #
51
+ def wait_available(timeout: WAIT_TIMEOUT)
52
+ deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + timeout
53
+ loop do
54
+ remaining = deadline - Process.clock_gettime(Process::CLOCK_MONOTONIC)
55
+ raise Error, "Secret Manager emulator readiness timed out" unless remaining > 0
56
+ return self if sdk_ready?([remaining, 2].min)
57
+ remaining = deadline - Process.clock_gettime(Process::CLOCK_MONOTONIC)
58
+ sleep [0.1, remaining].min if remaining > 0
59
+ end
60
+ end
61
+
62
+ #
63
+ # Delete all secrets and their versions in the selected test project.
64
+ # Collect names before deleting so pagination cannot skip entries.
65
+ #
66
+ # @return [self] after all listed secrets have been deleted
67
+ #
68
+ def clear
69
+ client.list_secrets(parent: "projects/#{project_id}").to_a.each do |secret|
70
+ client.delete_secret(name: secret.name)
71
+ end
72
+ self
73
+ end
74
+
75
+ #
76
+ # Release the shared gRPC channel after the integration run.
77
+ #
78
+ # @return [void]
79
+ #
80
+ def close
81
+ @channel.close
82
+ end
83
+
84
+ private
85
+
86
+ #
87
+ # Probe the list API; only unavailable/deadline errors become false.
88
+ #
89
+ # @param [Numeric] timeout per-request timeout in seconds
90
+ # @return [Boolean] true on success, false on a transient startup failure
91
+ #
92
+ def sdk_ready?(timeout)
93
+ # @type var retry_codes: Array[Integer]
94
+ retry_codes = []
95
+ client.list_secrets(
96
+ {parent: "projects/#{project_id}", page_size: 1},
97
+ {timeout: timeout, retry_policy: {retry_codes: retry_codes}}
98
+ ).each { |_| }
99
+ true
100
+ rescue Google::Cloud::UnavailableError, Google::Cloud::DeadlineExceededError
101
+ false
102
+ end
103
+ end
104
+ end
105
+ end
@@ -0,0 +1,64 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "logger"
4
+ begin
5
+ require "google/cloud/secret_manager"
6
+ require "google/cloud/secret_manager/v1"
7
+ rescue LoadError => e
8
+ warn e
9
+ module Google
10
+ module Cloud
11
+ module SecretManager
12
+ module V1
13
+ module SecretManagerService
14
+ class Client
15
+ def initialize(**kwargs)
16
+ end
17
+
18
+ def access_secret_version(**kwargs)
19
+ Struct.new(:payload).new(Struct.new(:data).new)
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
28
+
29
+ module Bindan
30
+ module Provider
31
+ class SecretManager
32
+ #
33
+ # Create a reader for global secrets in the supplied or environment project.
34
+ # Discard SDK request/response logs to avoid exposing secret values.
35
+ # Use a null logger: GAPIC treats nil as inheriting the default logger.
36
+ #
37
+ # @param [String, nil] project_id project ID or number; defaults to
38
+ # GOOGLE_CLOUD_PROJECT, then GCLOUD_PROJECT
39
+ # @param [Object] sdk client factory yielding configuration
40
+ # @param [Hash] kwargs SDK client settings (logger is always discarded)
41
+ #
42
+ def initialize(project_id: nil, sdk: ::Google::Cloud::SecretManager::V1::SecretManagerService::Client, **kwargs)
43
+ @project_id = project_id || ENV["GOOGLE_CLOUD_PROJECT"] || ENV["GCLOUD_PROJECT"]
44
+ @_secret_manager = sdk.new do |config|
45
+ kwargs.each { |key, value| config.public_send(:"#{key}=", value) }
46
+ config.logger = Logger.new(nil)
47
+ end
48
+ end
49
+ attr_reader :project_id
50
+
51
+ #
52
+ # Read the latest payload without caching or modifying its bytes.
53
+ #
54
+ # @param [String] key secret ID
55
+ # @return [String] secret payload bytes
56
+ # @raise [Google::Cloud::Error] when the SDK reports a service error
57
+ #
58
+ def [](key)
59
+ name = "projects/#{@project_id}/secrets/#{key}/versions/latest"
60
+ @_secret_manager.access_secret_version(name: name).payload.data
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,92 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "logger"
4
+ require "google/cloud/secret_manager/v1"
5
+
6
+ module Bindan
7
+ #
8
+ # Add versions separately from the read-only Secret Manager Provider.
9
+ # SDK errors propagate; avoid logging exception details that may contain secrets.
10
+ # A failed operation may have created a version; check before retrying.
11
+ #
12
+ # @example Use the helper directly without the CLI
13
+ # require "bindan/secret_manager_helper"
14
+ #
15
+ # helper = Bindan::SecretManagerHelper.new(project_id: "my-project")
16
+ # previous = helper.last_version("api-key")
17
+ # created = helper.update("api-key", data: File.binread("./api-key.bin"))
18
+ # previous&.name # Last created version before the update, if any.
19
+ # created.name # The newly created version.
20
+ # restored = helper.revert("api-key", version: 1)
21
+ #
22
+ class SecretManagerHelper
23
+ #
24
+ # Create a helper for adding versions of existing global secrets.
25
+ # Suppress SDK request/response logging and automatic write retries.
26
+ #
27
+ # @param [String, nil] project_id project ID or number; defaults to
28
+ # GOOGLE_CLOUD_PROJECT, then GCLOUD_PROJECT
29
+ # @param [Object] sdk client factory yielding configuration
30
+ # @param [Hash] kwargs SDK client settings (logger and retry policies are overridden)
31
+ #
32
+ def initialize(project_id: nil, sdk: ::Google::Cloud::SecretManager::V1::SecretManagerService::Client, **kwargs)
33
+ @project_id = project_id || ENV["GOOGLE_CLOUD_PROJECT"] || ENV["GCLOUD_PROJECT"]
34
+ @_secret_manager = sdk.new do |config|
35
+ kwargs.each { |key, value| config.public_send(:"#{key}=", value) }
36
+ config.logger = Logger.new(nil)
37
+ # GAPIC applies client-wide defaults after RPC defaults. Clear both so
38
+ # a caller-supplied retry policy cannot retry a version-creating write.
39
+ config.retry_policy = nil
40
+ config.rpcs.add_secret_version.retry_policy = nil
41
+ end
42
+ end
43
+ # @return [String, nil] explicit or environment project ID or number
44
+ attr_reader :project_id
45
+
46
+ #
47
+ # Read metadata for the last created version, regardless of its state.
48
+ # This does not resolve the latest alias. Listing is eventually consistent.
49
+ #
50
+ # @param [String] secret_id ID of an existing secret
51
+ # @return [Google::Cloud::SecretManager::V1::SecretVersion, nil] last created version, or nil if none
52
+ # @raise [Google::Cloud::Error] when the SDK reports a service error
53
+ #
54
+ def last_version(secret_id)
55
+ parent = "projects/#{@project_id}/secrets/#{secret_id}"
56
+ @_secret_manager.list_secret_versions(parent: parent).first
57
+ end
58
+
59
+ #
60
+ # Add the exact bytes as a new version and return the SDK's creation result.
61
+ # SDK failures propagate unchanged; no operation is retried here.
62
+ #
63
+ # @param [String] secret_id ID of an existing secret
64
+ # @param [String] data secret payload bytes
65
+ # @return [Google::Cloud::SecretManager::V1::SecretVersion] created version metadata
66
+ # @raise [Google::Cloud::Error] when the SDK reports a service error
67
+ #
68
+ def update(secret_id, data:)
69
+ parent = "projects/#{@project_id}/secrets/#{secret_id}"
70
+ @_secret_manager.add_secret_version(parent: parent, payload: {data: data.b})
71
+ end
72
+
73
+ #
74
+ # Copy an existing numerical version into a new version.
75
+ #
76
+ # @param [String] secret_id ID of an existing secret
77
+ # @param [String, Integer] version known-good positive numerical version, not an alias
78
+ # @return [Google::Cloud::SecretManager::V1::SecretVersion] created version metadata
79
+ # @raise [ArgumentError] when version is not a positive numerical version
80
+ # @raise [Google::Cloud::Error] when the SDK reports a service error
81
+ #
82
+ def revert(secret_id, version:)
83
+ unless /\A[1-9][0-9]*\z/.match?(version.to_s)
84
+ raise ArgumentError, "Secret version must be a positive numerical version"
85
+ end
86
+
87
+ name = "projects/#{@project_id}/secrets/#{secret_id}/versions/#{version}"
88
+ data = @_secret_manager.access_secret_version(name: name).payload.data
89
+ update(secret_id, data: data)
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,94 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "optparse"
4
+
5
+ module Bindan
6
+ class SecretUpdateCommand
7
+ USAGE = <<~TEXT
8
+ Usage: bindan-secret-update --secret SECRET_ID [--project PROJECT] (--file PATH | --from-version NUMBER)
9
+
10
+ Add a new version of an existing global Secret Manager secret.
11
+ --file preserves all bytes, including trailing newlines. Values are never printed.
12
+ --from-version copies a known-good numeric version into a new version.
13
+ Project defaults to GOOGLE_CLOUD_PROJECT, then GCLOUD_PROJECT.
14
+ Success prints the created version name; failure exits nonzero without retrying.
15
+ TEXT
16
+
17
+ #
18
+ # @param [Proc, nil] helper_factory factory accepting project_id:; defaults to SecretManagerHelper
19
+ # @param [IO] stdout destination for help and created version names
20
+ # @param [IO] stderr destination for errors without secret values
21
+ #
22
+ def initialize(helper_factory: nil, stdout: $stdout, stderr: $stderr)
23
+ @helper_factory = helper_factory || ->(**options) {
24
+ require_relative "secret_manager_helper"
25
+ SecretManagerHelper.new(**options)
26
+ }
27
+ @stdout = stdout
28
+ @stderr = stderr
29
+ end
30
+
31
+ #
32
+ # Parse arguments without performing I/O or modifying argv.
33
+ #
34
+ # @param [Array<String>] argv command-line arguments
35
+ # @return [Hash{Symbol => String, true}] parsed options, including :help when requested
36
+ # @raise [OptionParser::ParseError] when arguments are invalid or required options are missing
37
+ #
38
+ def self.parse(argv)
39
+ # @type var options: Hash[Symbol, untyped]
40
+ options = {}
41
+ parser = OptionParser.new do |opts|
42
+ opts.banner = USAGE
43
+ opts.on("--secret SECRET_ID") { |value| options[:secret] = value }
44
+ opts.on("--project PROJECT") { |value| options[:project_id] = value }
45
+ opts.on("--file PATH") { |value| options[:file] = value }
46
+ opts.on("--from-version NUMBER") { |value| options[:version] = value }
47
+ opts.on("-h", "--help") { options[:help] = true }
48
+ end
49
+ remaining = parser.parse(argv.dup)
50
+ return options if options[:help]
51
+
52
+ unless remaining.empty? && options[:secret] && (!!options[:file] != !!options[:version])
53
+ raise OptionParser::InvalidArgument, "Specify a secret and either a file or a source version"
54
+ end
55
+ options
56
+ end
57
+
58
+ #
59
+ # Add a version from file bytes or an existing numerical version.
60
+ # Error messages from files, credentials and SDKs may contain sensitive data.
61
+ # Keep those details out of the command's output, including backtraces.
62
+ #
63
+ # @param [Array<String>] argv command-line arguments
64
+ # @return [Integer] exit status: 0 for success/help, 2 for argument errors, 1 for operational failures
65
+ #
66
+ def run(argv)
67
+ options = self.class.parse(argv)
68
+ if options[:help]
69
+ @stdout.puts USAGE
70
+ return 0
71
+ end
72
+
73
+ # Read the entire file before creating a client or attempting a write.
74
+ data = File.binread(options[:file]) if options[:file]
75
+ helper = @helper_factory.call(project_id: options[:project_id])
76
+ version = if options[:file]
77
+ helper.update(options[:secret], data: data)
78
+ else
79
+ helper.revert(options[:secret], version: options[:version])
80
+ end
81
+ @stdout.puts version.name
82
+ 0
83
+ rescue OptionParser::ParseError
84
+ @stderr.puts USAGE
85
+ 2
86
+ rescue LoadError
87
+ @stderr.puts "Secret Manager SDK unavailable. Install google-cloud-secret_manager."
88
+ 1
89
+ rescue
90
+ @stderr.puts "Secret update failed. A version may have been created; check the target before retrying."
91
+ 1
92
+ end
93
+ end
94
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Bindan
4
- VERSION = "0.1.2"
4
+ VERSION = "0.2.1"
5
5
  end
@@ -11,7 +11,7 @@ module Bindan
11
11
 
12
12
  BACKOFF_MULTIPLIER: ::Float
13
13
 
14
- class Error < StandardError
14
+ class Error < Bindan::Error
15
15
  end
16
16
 
17
17
  @project_id: String
@@ -0,0 +1,49 @@
1
+ module GRPC
2
+ module Core
3
+ class Channel
4
+ def self.new: (String target, Hash[String, untyped] args, Symbol credentials) -> Channel
5
+ def close: () -> void
6
+ end
7
+ end
8
+ end
9
+
10
+ module Google
11
+ module Cloud
12
+ class Error < StandardError
13
+ end
14
+
15
+ class UnavailableError < Error
16
+ end
17
+
18
+ class DeadlineExceededError < Error
19
+ end
20
+ end
21
+ end
22
+
23
+ module Bindan
24
+ module Emulator
25
+ class SecretManagerController
26
+ WAIT_TIMEOUT: 60
27
+
28
+ class Error < Bindan::Error
29
+ end
30
+
31
+ @project_id: String
32
+ @channel: GRPC::Core::Channel
33
+ @client: untyped
34
+
35
+ attr_reader project_id: String
36
+ attr_reader client: untyped
37
+
38
+ def initialize: (?project_id: String) -> void
39
+ def endpoint: () -> String
40
+ def wait_available: (?timeout: Integer | Float) -> SecretManagerController
41
+ def clear: () -> SecretManagerController
42
+ def close: () -> void
43
+
44
+ private
45
+
46
+ def sdk_ready?: (Integer | Float timeout) -> bool
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,28 @@
1
+ module Google
2
+ module Cloud
3
+ module SecretManager
4
+ module V1
5
+ module SecretManagerService
6
+ class Client
7
+ def initialize: (**untyped kwargs) -> void
8
+ def self.new: () { (untyped config) -> void } -> untyped
9
+ def access_secret_version: (**untyped kwargs) -> untyped
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
16
+
17
+ module Bindan
18
+ module Provider
19
+ class SecretManager
20
+ @project_id: String?
21
+ @_secret_manager: untyped
22
+
23
+ def initialize: (?project_id: String?, ?sdk: untyped, **untyped kwargs) -> void
24
+ attr_reader project_id: String?
25
+ def []: (String key) -> untyped
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,12 @@
1
+ module Bindan
2
+ class SecretManagerHelper
3
+ @project_id: String?
4
+ @_secret_manager: untyped
5
+
6
+ def initialize: (?project_id: String?, ?sdk: untyped, **untyped kwargs) -> void
7
+ attr_reader project_id: String?
8
+ def last_version: (String secret_id) -> untyped
9
+ def update: (String secret_id, data: String) -> untyped
10
+ def revert: (String secret_id, version: String | Integer) -> untyped
11
+ end
12
+ end
@@ -0,0 +1,8 @@
1
+ module Bindan
2
+ class SecretUpdateCommand
3
+ USAGE: String
4
+ def self.parse: (Array[String] argv) -> Hash[Symbol, untyped]
5
+ def initialize: (?helper_factory: untyped, ?stdout: untyped, ?stderr: untyped) -> void
6
+ def run: (Array[String] argv) -> Integer
7
+ end
8
+ end
metadata CHANGED
@@ -1,23 +1,24 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bindan
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - wtnabe
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2026-11-12 00:00:00.000000000 Z
11
+ date: 2026-11-15 00:00:00.000000000 Z
12
12
  dependencies: []
13
- description: Bindan is a Ruby gem for building single configuration object from various
14
- sources with providers (that is bundled Google Cloud Storage and Firestore platform
15
- by default). It provides a flexible way to manage application settings, supporting
16
- lazy initialization and seamless switching between development (using emulators)
17
- and production (eg. actual GCP services) environments.
13
+ description: Bindan is a Ruby gem for building a single configuration object from
14
+ various sources with providers (including bundled Google Cloud Storage, Firestore,
15
+ and Secret Manager providers). It provides a flexible way to manage application
16
+ settings, supporting lazy initialization and seamless switching between development
17
+ (using emulators) and production (eg. actual GCP services) environments.
18
18
  email:
19
19
  - 18510+wtnabe@users.noreply.github.com
20
- executables: []
20
+ executables:
21
+ - bindan-secret-update
21
22
  extensions: []
22
23
  extra_rdoc_files: []
23
24
  files:
@@ -30,24 +31,35 @@ files:
30
31
  - Rakefile
31
32
  - Steepfile
32
33
  - compose.yml
34
+ - docs/configuration-rollout.md
35
+ - docs/secret-manager-operations.md
36
+ - exe/bindan-secret-update
33
37
  - lib/bindan.rb
34
38
  - lib/bindan/emulators/firestore_controller.rb
35
39
  - lib/bindan/emulators/gcs_server_controller.rb
40
+ - lib/bindan/emulators/secret_manager_controller.rb
36
41
  - lib/bindan/error.rb
37
42
  - lib/bindan/providers.rb
38
43
  - lib/bindan/providers/envvar.rb
39
44
  - lib/bindan/providers/firestore.rb
45
+ - lib/bindan/providers/secret_manager.rb
40
46
  - lib/bindan/providers/storage.rb
47
+ - lib/bindan/secret_manager_helper.rb
48
+ - lib/bindan/secret_update_command.rb
41
49
  - lib/bindan/version.rb
42
50
  - rbs_collection.lock.yaml
43
51
  - rbs_collection.yaml
44
52
  - sig/bindan.rbs
45
53
  - sig/bindan/emulators/firestore_controller.rbs
46
54
  - sig/bindan/emulators/gcs_server_controller.rbs
55
+ - sig/bindan/emulators/secret_manager_controller.rbs
47
56
  - sig/bindan/error.rbs
48
57
  - sig/bindan/providers/envvar.rbs
49
58
  - sig/bindan/providers/firestore.rbs
59
+ - sig/bindan/providers/secret_manager.rbs
50
60
  - sig/bindan/providers/storage.rbs
61
+ - sig/bindan/secret_manager_helper.rbs
62
+ - sig/bindan/secret_update_command.rbs
51
63
  - sig/bindan/version.rbs
52
64
  homepage: https://github.com/wtnabe/bindan
53
65
  licenses: []