bindan 0.1.1 → 0.2.0
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 +4 -4
- data/CHANGELOG.md +28 -0
- data/README.md +55 -18
- data/Rakefile +7 -1
- data/compose.yml +26 -2
- data/docs/secret-manager-operations.md +76 -0
- data/lib/bindan/emulators/firestore_controller.rb +87 -117
- data/lib/bindan/emulators/gcs_server_controller.rb +19 -82
- data/lib/bindan/emulators/secret_manager_controller.rb +104 -0
- data/lib/bindan/providers/firestore.rb +28 -9
- data/lib/bindan/providers/secret_manager.rb +64 -0
- data/lib/bindan/providers/storage.rb +18 -3
- data/lib/bindan/version.rb +1 -1
- data/sig/bindan/emulators/firestore_controller.rbs +20 -49
- data/sig/bindan/emulators/gcs_server_controller.rbs +8 -45
- data/sig/bindan/emulators/secret_manager_controller.rbs +49 -0
- data/sig/bindan/providers/firestore.rbs +2 -0
- data/sig/bindan/providers/secret_manager.rbs +28 -0
- metadata +12 -7
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 3f8436f7249259c3a94776cb3698fe1afe49446e1b21c7e45a8e2930334602fe
|
|
4
|
+
data.tar.gz: 1241b3b53ba1633c55dd817d59b0300eda7713baccda8611c4bf89488e29b2ff
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 92f7df51c973455ec67ef6381b0534f5dd123ca8e336ae24a5cb80f1f9c646222a7db582db41bd31289408251fef0fb3bc23c9fc76d23b6f951380ee1bf8d767
|
|
7
|
+
data.tar.gz: 31499462bfff33ed2f6341db17d5ba9e0dae3215ff70afd1aa5d1045e8f23026097f3083d1e13badefe078496c317ee3cb08ba2d1dc522631f8d39626057f48e
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,33 @@
|
|
|
1
1
|
## [Unreleased]
|
|
2
2
|
|
|
3
|
+
## [0.2.0] - 2026-11-13
|
|
4
|
+
|
|
5
|
+
### Added
|
|
6
|
+
|
|
7
|
+
- Add `Bindan::Provider::SecretManager` to read configuration from Google Cloud Secret Manager.
|
|
8
|
+
|
|
9
|
+
## [0.1.2] - 2026-11-12
|
|
10
|
+
|
|
11
|
+
### Changed
|
|
12
|
+
|
|
13
|
+
- Separate Docker-free unit tests from emulator-backed integration tests.
|
|
14
|
+
- Manage GCS and Firestore emulators with Docker Compose. CI starts and stops the services; local integration tests use externally started services.
|
|
15
|
+
- Clear Firestore test data between cases instead of restarting the emulator for each test.
|
|
16
|
+
- Extract Firestore document path resolution without changing lookup behavior.
|
|
17
|
+
|
|
18
|
+
### Fixed
|
|
19
|
+
|
|
20
|
+
- Correct the multiple-Provider README example and clarify Storage bucket names and Firestore collection paths.
|
|
21
|
+
- Restore Cloud Storage integration coverage in CI and constrain development dependencies for Ruby 3.0 compatibility.
|
|
22
|
+
|
|
23
|
+
### Development compatibility
|
|
24
|
+
|
|
25
|
+
- Remove `bin/emulator` and controller startup/shutdown APIs. Use `docker compose up` / `down` instead; host Java and Google Cloud CLI are no longer required. Firestore import/export is no longer supported by the development tooling.
|
|
26
|
+
|
|
27
|
+
## [0.1.1] - 2025-08-07
|
|
28
|
+
|
|
29
|
+
- add Class-extend use
|
|
30
|
+
|
|
3
31
|
## [0.1.0] - 2025-08-03
|
|
4
32
|
|
|
5
33
|
- Initial release
|
data/README.md
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
[](https://badge.fury.io/rb/bindan)
|
|
3
3
|
[](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 (
|
|
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,20 +77,23 @@ 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
|
|
82
84
|
|
|
83
85
|
```ruby
|
|
84
86
|
require "bindan"
|
|
85
|
-
require "google/
|
|
86
|
-
require "google/
|
|
87
|
+
require "google/cloud/storage"
|
|
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
|
-
storage: Bindan::Provider::Storage.new(bucket: "my-
|
|
92
|
-
firestore: Bindan::Provider::Firestore.new(collection: "
|
|
94
|
+
storage: Bindan::Provider::Storage.new("config-bucket", 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,34 +102,42 @@ 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
|
|
|
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.
|
|
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
|
+
For update and revert precautions and commands, see
|
|
123
|
+
[Secret Manager operations](docs/secret-manager-operations.md).
|
|
124
|
+
|
|
111
125
|
## Development
|
|
112
126
|
|
|
113
|
-
After checking out the repo, run `bin/setup` to install dependencies
|
|
127
|
+
After checking out the repo, run `bin/setup` to install development dependencies and pull the emulator images. It does not start the containers.
|
|
128
|
+
|
|
129
|
+
```bash
|
|
130
|
+
bin/setup
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
114
134
|
|
|
115
135
|
### Prerequisites for Development
|
|
116
136
|
|
|
117
|
-
|
|
137
|
+
The development environment includes the following tools for running heavy integration tests locally:
|
|
118
138
|
|
|
119
|
-
1. **
|
|
120
|
-
2. **
|
|
121
|
-
3. **Firestore Emulator:**
|
|
122
|
-
```bash
|
|
123
|
-
gcloud components install cloud-firestore-emulator
|
|
124
|
-
```
|
|
125
|
-
4. **fake-gcs-server:**
|
|
126
|
-
```bash
|
|
127
|
-
docker pull fsouza/fake-gcs-server
|
|
128
|
-
```
|
|
139
|
+
1. **Docker with Docker Compose:** [Get Docker](https://docs.docker.com/get-docker/), with its daemon running.
|
|
140
|
+
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
141
|
|
|
130
142
|
### Custom Provider
|
|
131
143
|
|
|
@@ -141,12 +153,37 @@ end
|
|
|
141
153
|
|
|
142
154
|
### Running Tests
|
|
143
155
|
|
|
144
|
-
|
|
156
|
+
Use the lightweight unit tests during normal development. They do not start emulators:
|
|
145
157
|
|
|
146
158
|
```bash
|
|
147
159
|
bundle exec rake spec
|
|
148
160
|
```
|
|
149
161
|
|
|
162
|
+
Run the heavy integration tests locally when verifying Provider behavior against the real SDKs and emulators. Start the containers explicitly before running the suite:
|
|
163
|
+
|
|
164
|
+
```bash
|
|
165
|
+
docker compose up --detach --wait
|
|
166
|
+
bundle exec rake spec:integration
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
The tests wait for readiness but never start or stop containers. Stop them yourself when no longer needed:
|
|
170
|
+
|
|
171
|
+
```bash
|
|
172
|
+
docker compose down
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
CI runs both suites and manages Compose startup, logs and cleanup in the workflow, including cleanup after failures.
|
|
176
|
+
|
|
177
|
+
Integration tests run serially against Storage on `127.0.0.1:4443`, Firestore on
|
|
178
|
+
`127.0.0.1:8080`, and Secret Manager on `127.0.0.1:9090`. Each Firestore case clears
|
|
179
|
+
the `bindan-test` project's default database before seeding data. Secret Manager
|
|
180
|
+
cases clear secrets in the `bindan-test` project before and after each case.
|
|
181
|
+
Use these emulators for disposable, non-sensitive test data only.
|
|
182
|
+
The Firestore and Secret Manager controllers wait for an SDK request to succeed,
|
|
183
|
+
not merely a TCP connection. The suite does not import or export emulator data.
|
|
184
|
+
To use dummy Storage credentials without local GCP credentials, run
|
|
185
|
+
`CI=true bundle exec rake spec:integration`.
|
|
186
|
+
|
|
150
187
|
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).
|
|
151
188
|
|
|
152
189
|
## Contributing
|
data/Rakefile
CHANGED
|
@@ -5,7 +5,13 @@ require "minitest/test_task"
|
|
|
5
5
|
|
|
6
6
|
Minitest::TestTask.create(:spec) do |t|
|
|
7
7
|
t.libs << "spec"
|
|
8
|
-
t.test_globs = "spec/**/*_spec.rb"
|
|
8
|
+
t.test_globs = Dir.glob("spec/**/*_spec.rb").reject { |path| path.start_with?("spec/integration/") }
|
|
9
|
+
t.warning = false
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
Minitest::TestTask.create("spec:integration") do |t|
|
|
13
|
+
t.libs << "spec"
|
|
14
|
+
t.test_globs = "spec/integration/**/*_spec.rb"
|
|
9
15
|
t.warning = false
|
|
10
16
|
end
|
|
11
17
|
|
data/compose.yml
CHANGED
|
@@ -3,10 +3,34 @@ services:
|
|
|
3
3
|
image: fsouza/fake-gcs-server:latest
|
|
4
4
|
command: -scheme http -port 4443 -host 0.0.0.0
|
|
5
5
|
ports:
|
|
6
|
-
- "4443:4443"
|
|
6
|
+
- "127.0.0.1:4443:4443"
|
|
7
|
+
volumes:
|
|
8
|
+
- "./spec/support/storage:/data/-config:ro"
|
|
7
9
|
healthcheck:
|
|
8
10
|
# The fake-gcs-server is ready when we can query the bucket list endpoint.
|
|
9
|
-
test: ["CMD", "
|
|
11
|
+
test: ["CMD", "wget", "-q", "-O", "/dev/null", "http://127.0.0.1:4443/storage/v1/b"]
|
|
10
12
|
interval: 1s
|
|
11
13
|
timeout: 5s
|
|
12
14
|
retries: 20
|
|
15
|
+
firestore:
|
|
16
|
+
image: gcr.io/google.com/cloudsdktool/google-cloud-cli:emulators
|
|
17
|
+
command:
|
|
18
|
+
- gcloud
|
|
19
|
+
- emulators
|
|
20
|
+
- firestore
|
|
21
|
+
- start
|
|
22
|
+
- --host-port=0.0.0.0:8080
|
|
23
|
+
- --database-mode=firestore-native
|
|
24
|
+
- --edition=standard
|
|
25
|
+
ports:
|
|
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,76 @@
|
|
|
1
|
+
# Secret Manager operations
|
|
2
|
+
|
|
3
|
+
## Update and revert: what matters
|
|
4
|
+
|
|
5
|
+
- **Update by adding a new version.** This provider reads `latest`, which selects
|
|
6
|
+
the newest created version, not the newest enabled one.
|
|
7
|
+
- **Reverting requires another update.** Record the known-good version before
|
|
8
|
+
making a change. To restore its value, add that value as a new version.
|
|
9
|
+
Disabling or destroying the newest version does not restore the previous one:
|
|
10
|
+
GCP's `latest` still selects the newest created version and reading it fails.
|
|
11
|
+
The emulator may return an older enabled version instead; GCP does not.
|
|
12
|
+
- **Do not expose or accidentally alter the value while updating or reverting.**
|
|
13
|
+
Logs and shell history can retain secrets; added or removed newlines can change
|
|
14
|
+
their meaning. If an update fails or times out, check whether it created a
|
|
15
|
+
version before retrying.
|
|
16
|
+
|
|
17
|
+
## Manual command examples
|
|
18
|
+
|
|
19
|
+
These examples use Bash and `gcloud`, including Cloud Shell. They modify an
|
|
20
|
+
existing secret; check the account and target before running them, and serialize
|
|
21
|
+
updates to the same secret. The operator needs permissions to
|
|
22
|
+
access/add versions and read version metadata, separate from the application's
|
|
23
|
+
read-only access. Use non-sensitive test data before production use.
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
set +x
|
|
27
|
+
export CLOUDSDK_CORE_LOG_HTTP=false CLOUDSDK_CORE_VERBOSITY=error
|
|
28
|
+
PROJECT_ID="my-project"
|
|
29
|
+
SECRET_ID="api-key"
|
|
30
|
+
gcloud auth list --filter=status:ACTIVE --format='value(account)'
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
### Update
|
|
34
|
+
|
|
35
|
+
Prepare a protected file with the exact desired bytes, then add a version.
|
|
36
|
+
Avoid `echo` or embedding secrets in shell commands/history. Record the returned
|
|
37
|
+
version name and the previous known-good version number.
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
gcloud secrets versions add "$SECRET_ID" --project="$PROJECT_ID" \
|
|
41
|
+
--data-file="./api-key.bin" --format='value(name)'
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
### Revert
|
|
45
|
+
|
|
46
|
+
Copy a known-good numeric version into a new version, then verify its bytes.
|
|
47
|
+
|
|
48
|
+
```bash
|
|
49
|
+
(
|
|
50
|
+
set -euo pipefail
|
|
51
|
+
umask 077
|
|
52
|
+
SOURCE_VERSION="1" # Replace with the known-good version number.
|
|
53
|
+
workdir="$(mktemp -d)"
|
|
54
|
+
trap 'rm -rf -- "$workdir"' EXIT
|
|
55
|
+
|
|
56
|
+
gcloud secrets versions access "$SOURCE_VERSION" \
|
|
57
|
+
--secret="$SECRET_ID" --project="$PROJECT_ID" --out-file="$workdir/payload"
|
|
58
|
+
created="$(gcloud secrets versions add "$SECRET_ID" \
|
|
59
|
+
--project="$PROJECT_ID" --data-file="$workdir/payload" --format='value(name)')"
|
|
60
|
+
[[ "$created" =~ /versions/[0-9]+$ ]] || exit 1
|
|
61
|
+
gcloud secrets versions access "${created##*/}" \
|
|
62
|
+
--secret="$SECRET_ID" --project="$PROJECT_ID" --out-file="$workdir/check"
|
|
63
|
+
cmp -s "$workdir/payload" "$workdir/check"
|
|
64
|
+
printf 'Created and verified: %s\n' "$created"
|
|
65
|
+
)
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
`--out-file` preserves raw bytes, including trailing newlines. Temporary files
|
|
69
|
+
contain plaintext; use a protected environment. Cleanup is not secure erasure.
|
|
70
|
+
|
|
71
|
+
If the command fails or verification reports a mismatch, check which version was
|
|
72
|
+
created and whether its bytes match the intended value before retrying. Do not
|
|
73
|
+
assume a failed command means no version was added.
|
|
74
|
+
|
|
75
|
+
See [version access](https://cloud.google.com/sdk/gcloud/reference/secrets/versions/access)
|
|
76
|
+
and [version addition](https://cloud.google.com/sdk/gcloud/reference/secrets/versions/add).
|
|
@@ -1,151 +1,121 @@
|
|
|
1
1
|
require_relative "../error"
|
|
2
|
-
require "
|
|
3
|
-
require "open3"
|
|
4
|
-
require "socket"
|
|
2
|
+
require "net/http"
|
|
5
3
|
require "timeout"
|
|
6
|
-
require "uri"
|
|
7
4
|
|
|
8
5
|
module Bindan
|
|
9
6
|
module Emulator
|
|
10
7
|
#
|
|
11
|
-
# Represents and controls a single instance of the Google Cloud Firestore emulator
|
|
12
|
-
# This class
|
|
13
|
-
# separating process management from the test execution flow.
|
|
8
|
+
# Represents and controls a single instance of the Google Cloud Firestore emulator service.
|
|
9
|
+
# This class checks readiness and clears test data without managing containers.
|
|
14
10
|
#
|
|
15
11
|
class FirestoreController
|
|
16
|
-
WAIT_TIMEOUT =
|
|
12
|
+
WAIT_TIMEOUT = 60 # seconds
|
|
17
13
|
INITIAL_BACKOFF_KEY_SEC = 0.1 # seconds
|
|
18
14
|
BACKOFF_MULTIPLIER = 1.5
|
|
19
15
|
|
|
20
|
-
class
|
|
21
|
-
#
|
|
22
|
-
# @return [Array<String, Integer>]
|
|
23
|
-
#
|
|
24
|
-
def host_and_port(host = nil, port = nil)
|
|
25
|
-
host, port = ENV["FIRESTORE_EMULATOR_HOST"].to_s.split(":") if ENV["FIRESTORE_EMULATOR_HOST"]
|
|
16
|
+
class Error < StandardError; end
|
|
26
17
|
|
|
27
|
-
|
|
28
|
-
port ||= 8080
|
|
18
|
+
attr_reader :project_id
|
|
29
19
|
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
# @param [String] import
|
|
37
|
-
# @param [String] export
|
|
38
|
-
# @param [String] host
|
|
39
|
-
# @param [Integer] port
|
|
40
|
-
# @param [bool] close_io
|
|
41
|
-
# @raise [Errno]
|
|
42
|
-
# @return [FirestoreEmulatorController] An instance to manage the emulator lifecycle.
|
|
43
|
-
#
|
|
44
|
-
def start(import: nil, export: nil, host: nil, port: nil, close_io: true)
|
|
45
|
-
host, port = host_and_port(host, port)
|
|
46
|
-
|
|
47
|
-
puts " Starting Firestore emulator ..." unless close_io
|
|
48
|
-
kill_process_if_already_exists(host: host, port: port, with_message: !close_io)
|
|
49
|
-
|
|
50
|
-
#
|
|
51
|
-
# This command is known to launch a background Java process and then exit.
|
|
52
|
-
# Trying keep process info with process group
|
|
53
|
-
#
|
|
54
|
-
cmd = "gcloud emulators firestore start --host-port=#{host}:#{port}"
|
|
55
|
-
cmd += " --import-data=#{import}" if import
|
|
56
|
-
cmd += " --export-on-exit=#{export}" if export
|
|
57
|
-
opts = close_io ? {out: "/dev/null", err: "/dev/null"} : {}
|
|
58
|
-
pid = Process.spawn(*cmd, pgroup: true, **opts) # steep:ignore
|
|
59
|
-
|
|
60
|
-
puts " Emulator process initiated." unless close_io
|
|
61
|
-
new(pid, host, port)
|
|
62
|
-
end
|
|
63
|
-
|
|
64
|
-
#
|
|
65
|
-
# kill process group
|
|
66
|
-
#
|
|
67
|
-
# @param [Integer] pid
|
|
68
|
-
# @param [bool] with_message
|
|
69
|
-
# @return [void]
|
|
70
|
-
#
|
|
71
|
-
def stop(pid, with_message: true)
|
|
72
|
-
puts "\n=> Stopping Firestore emulator..." if with_message
|
|
73
|
-
|
|
74
|
-
begin
|
|
75
|
-
Process.kill("TERM", -pid)
|
|
76
|
-
Process.wait(-pid)
|
|
77
|
-
rescue Errno::ESRCH, Errno::ECHILD
|
|
78
|
-
# Process was already killed or doesn't exist, which is fine.
|
|
79
|
-
end
|
|
80
|
-
|
|
81
|
-
puts " Emulator process terminated." if with_message
|
|
82
|
-
end
|
|
83
|
-
|
|
84
|
-
#
|
|
85
|
-
# @param [Integer] host
|
|
86
|
-
# @param [Integer] port
|
|
87
|
-
#
|
|
88
|
-
def kill_process_if_already_exists(host:, port:, with_message: true)
|
|
89
|
-
TCPSocket.new(host, port).close
|
|
90
|
-
|
|
91
|
-
# `lsof -Fg -i:PORT` returns PIDs of the process listening on the port.
|
|
92
|
-
process_ids, = Open3.capture3(*"lsof -Fg -g -s TCP:LISTEN -i :#{port}".split(" "))
|
|
93
|
-
process_group = process_ids.lines.map(&:chomp).find { |id| id =~ /^g([0-9]+)/ }
|
|
94
|
-
|
|
95
|
-
if process_group
|
|
96
|
-
id = process_group[1..].to_i # strip first letter
|
|
97
|
-
puts " Found emulator process group #{id} on port #{port}. Terminating." if with_message
|
|
98
|
-
stop(id, with_message: false)
|
|
99
|
-
end
|
|
100
|
-
rescue Errno::ECONNREFUSED
|
|
101
|
-
# noop
|
|
102
|
-
end
|
|
103
|
-
end # of class methods
|
|
20
|
+
#
|
|
21
|
+
# Select the project used by the integration tests.
|
|
22
|
+
#
|
|
23
|
+
def initialize(project_id: "bindan-test")
|
|
24
|
+
@project_id = project_id
|
|
25
|
+
end
|
|
104
26
|
|
|
105
27
|
#
|
|
106
|
-
# @
|
|
28
|
+
# @return [String] SDK endpoint (host:port, without an HTTP scheme)
|
|
107
29
|
#
|
|
108
|
-
def
|
|
109
|
-
|
|
110
|
-
@host = host
|
|
111
|
-
@port = port
|
|
30
|
+
def endpoint
|
|
31
|
+
"127.0.0.1:8080"
|
|
112
32
|
end
|
|
113
33
|
|
|
114
34
|
#
|
|
115
|
-
#
|
|
35
|
+
# Wait for an SDK read, retrying false with bounded exponential backoff.
|
|
116
36
|
#
|
|
117
|
-
# @
|
|
37
|
+
# @return [self] after a successful SDK read
|
|
38
|
+
# @raise [Error] when the overall deadline expires, including during a check
|
|
118
39
|
# @param [number] timeout
|
|
119
40
|
# @param [number] backoff
|
|
120
41
|
# @param [number] multiplier
|
|
121
|
-
#
|
|
42
|
+
# Failures/interruption propagate; container lifecycle is managed externally.
|
|
122
43
|
#
|
|
123
44
|
def wait_available(timeout: WAIT_TIMEOUT, backoff: INITIAL_BACKOFF_KEY_SEC, multiplier: BACKOFF_MULTIPLIER, with_message: true)
|
|
124
|
-
|
|
45
|
+
deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + timeout
|
|
46
|
+
@readiness_error = nil
|
|
47
|
+
puts " Waiting for Firestore SDK on #{endpoint}..." if with_message
|
|
48
|
+
loop do
|
|
49
|
+
remaining = deadline - Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
50
|
+
raise Error, "Firestore readiness timed out: #{@readiness_error}" unless remaining > 0
|
|
51
|
+
return self if Timeout.timeout(remaining) { sdk_ready?([remaining, 2].min) }
|
|
52
|
+
remaining = deadline - Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
53
|
+
sleep [backoff, remaining].min if remaining > 0
|
|
54
|
+
backoff *= multiplier
|
|
55
|
+
end
|
|
56
|
+
rescue Timeout::Error => error
|
|
57
|
+
raise Error, "Firestore readiness timed out: #{error.message}"
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
#
|
|
61
|
+
# Clear only this project's default database and wait for DELETE completion.
|
|
62
|
+
#
|
|
63
|
+
# @return [self] after HTTP 200, before the next test seeds any data
|
|
64
|
+
# @raise [Error] for an unsuccessful HTTP status
|
|
65
|
+
# HTTP/timeout errors propagate; no lifecycle or other-project operations occur.
|
|
66
|
+
#
|
|
67
|
+
def clear(timeout: 10)
|
|
68
|
+
path = "/emulator/v1/projects/#{project_id}/databases/(default)/documents"
|
|
69
|
+
response = Timeout.timeout(timeout) { delete_documents(path, timeout) }
|
|
70
|
+
raise Error, "Firestore clear #{project_id} failed: HTTP #{response.code}: #{response.body}" unless response.code == "200"
|
|
71
|
+
self
|
|
72
|
+
end
|
|
125
73
|
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
74
|
+
private
|
|
75
|
+
|
|
76
|
+
#
|
|
77
|
+
# Check a read through the V1 public API, with per-call timeout and no SDK retry.
|
|
78
|
+
# Keep this lower-level boundary here: high-level doc.get can retry past its timeout.
|
|
79
|
+
#
|
|
80
|
+
# @return [Boolean] true on a completed read (a missing document is also valid),
|
|
81
|
+
# false only for transient unavailable/deadline errors during startup
|
|
82
|
+
# Other SDK/configuration errors propagate; no document is written.
|
|
83
|
+
# Each attempt closes its channel; wait_available also enforces an overall deadline.
|
|
84
|
+
#
|
|
85
|
+
def sdk_ready?(timeout)
|
|
86
|
+
require "google/cloud/firestore/v1"
|
|
87
|
+
require "grpc"
|
|
88
|
+
channel = GRPC::Core::Channel.new(endpoint, {}, :this_channel_is_insecure)
|
|
89
|
+
client = Google::Cloud::Firestore::V1::Firestore::Client.new do |config|
|
|
90
|
+
config.endpoint = endpoint
|
|
91
|
+
config.credentials = channel
|
|
139
92
|
end
|
|
140
|
-
|
|
141
|
-
|
|
93
|
+
database = "projects/#{project_id}/databases/(default)"
|
|
94
|
+
client.batch_get_documents(
|
|
95
|
+
{database: database, documents: ["#{database}/documents/bindan-control/readiness"]},
|
|
96
|
+
{timeout: timeout, retry_policy: {retry_codes: []}}
|
|
97
|
+
).each { |_| }
|
|
98
|
+
@readiness_error = nil
|
|
99
|
+
true
|
|
100
|
+
rescue Google::Cloud::UnavailableError, Google::Cloud::DeadlineExceededError => error
|
|
101
|
+
@readiness_error = error
|
|
102
|
+
false
|
|
103
|
+
ensure
|
|
104
|
+
channel&.close
|
|
142
105
|
end
|
|
143
106
|
|
|
144
107
|
#
|
|
145
|
-
#
|
|
108
|
+
# Send clear's local DELETE without environment proxies or redirect following.
|
|
109
|
+
#
|
|
110
|
+
# @return [Net::HTTPResponse] the completed response; clear validates its status
|
|
111
|
+
# Connection and timeout errors propagate to the caller.
|
|
146
112
|
#
|
|
147
|
-
def
|
|
148
|
-
|
|
113
|
+
def delete_documents(path, timeout)
|
|
114
|
+
http = Net::HTTP.new("127.0.0.1", 8080, nil) # Explicitly disable environment proxies.
|
|
115
|
+
http.open_timeout = timeout
|
|
116
|
+
http.read_timeout = timeout
|
|
117
|
+
http.write_timeout = timeout
|
|
118
|
+
http.request(Net::HTTP::Delete.new(path))
|
|
149
119
|
end
|
|
150
120
|
end
|
|
151
121
|
end
|
|
@@ -1,89 +1,23 @@
|
|
|
1
1
|
require_relative "../error"
|
|
2
|
-
require "
|
|
2
|
+
require "net/http"
|
|
3
3
|
require "timeout"
|
|
4
|
-
require "open3"
|
|
5
4
|
|
|
6
5
|
module Bindan
|
|
7
6
|
module Emulator
|
|
8
7
|
class GcsServerController
|
|
9
8
|
class ContainerCannotOpenError < Error; end
|
|
10
9
|
|
|
11
|
-
CONTAINER_NAME = "gcs-server"
|
|
12
10
|
WAIT_TIMEOUT = 15 # seconds
|
|
13
11
|
INITIAL_BACKOFF_KEY_SEC = 0.1 # seconds
|
|
14
12
|
BACKOFF_MULTIPLIER = 1.5
|
|
15
13
|
|
|
16
|
-
class << self
|
|
17
|
-
#
|
|
18
|
-
# @raise [Errno]
|
|
19
|
-
# @param [String] folder
|
|
20
|
-
# @param [Integer] [port]
|
|
21
|
-
# @param [String] [name]
|
|
22
|
-
# @return [GcsServerController]
|
|
23
|
-
#
|
|
24
|
-
def start(folder:, port: 4443, name: CONTAINER_NAME, close_io: true)
|
|
25
|
-
FileUtils.mkdir_p(folder)
|
|
26
|
-
unless running?(name: name)
|
|
27
|
-
opts = close_io ? {out: "/dev/null", err: "/dev/null"} : {}
|
|
28
|
-
Process.spawn( # steep:ignore
|
|
29
|
-
*"docker run --rm --name #{name} -p #{port}:4443 -v #{folder}:/data fsouza/fake-gcs-server -scheme http".split(" "),
|
|
30
|
-
**opts
|
|
31
|
-
)
|
|
32
|
-
end
|
|
33
|
-
|
|
34
|
-
new(name: name, folder: folder)
|
|
35
|
-
end
|
|
36
|
-
|
|
37
|
-
#
|
|
38
|
-
# @raise [IOError]
|
|
39
|
-
# @param [String] name
|
|
40
|
-
# @return [bool]
|
|
41
|
-
#
|
|
42
|
-
def running?(name:)
|
|
43
|
-
_stdin, stdout, stderr, = Open3.popen3("docker container list -f name=#{name}")
|
|
44
|
-
|
|
45
|
-
begin
|
|
46
|
-
# container list not only headers
|
|
47
|
-
stdout.readlines.size > 1
|
|
48
|
-
rescue IOError => e
|
|
49
|
-
warn "in #{self}"
|
|
50
|
-
warn " " + stderr.read
|
|
51
|
-
raise ContainerCannotOpenError.new e
|
|
52
|
-
end
|
|
53
|
-
end
|
|
54
|
-
|
|
55
|
-
#
|
|
56
|
-
# @param [String] name
|
|
57
|
-
# @raise [Errno]
|
|
58
|
-
# @return [void]
|
|
59
|
-
#
|
|
60
|
-
def stop(name:)
|
|
61
|
-
`docker container stop #{name}`
|
|
62
|
-
end
|
|
63
|
-
end # of class methods
|
|
64
|
-
|
|
65
|
-
#
|
|
66
|
-
# @param [String] name
|
|
67
|
-
# @param [String] folder
|
|
68
14
|
#
|
|
69
|
-
def initialize(name:, folder:)
|
|
70
|
-
@container_name = name
|
|
71
|
-
@folder = folder
|
|
72
|
-
end
|
|
73
|
-
|
|
74
|
-
#
|
|
75
|
-
# @return
|
|
76
|
-
#
|
|
77
|
-
def stop
|
|
78
|
-
self.class.stop(name: @container_name)
|
|
79
|
-
end
|
|
80
|
-
|
|
81
|
-
#
|
|
82
|
-
# @param [String] name
|
|
83
15
|
# @return [bool]
|
|
84
16
|
#
|
|
85
17
|
def running?
|
|
86
|
-
|
|
18
|
+
read_status.code == "200"
|
|
19
|
+
rescue Errno::ECONNREFUSED, Errno::ECONNRESET, Net::OpenTimeout, Net::ReadTimeout
|
|
20
|
+
false
|
|
87
21
|
end
|
|
88
22
|
|
|
89
23
|
#
|
|
@@ -93,26 +27,29 @@ module Bindan
|
|
|
93
27
|
# @param [number] timeout
|
|
94
28
|
# @param [number] backoff
|
|
95
29
|
# @param [number] multiplier
|
|
96
|
-
# @return [
|
|
30
|
+
# @return [self]
|
|
97
31
|
#
|
|
98
32
|
def wait_available(timeout: WAIT_TIMEOUT, backoff: INITIAL_BACKOFF_KEY_SEC, multiplier: BACKOFF_MULTIPLIER)
|
|
99
|
-
last_rescued_error = nil
|
|
100
|
-
|
|
101
|
-
count = 1
|
|
102
33
|
Timeout.timeout(timeout) do
|
|
103
34
|
loop do
|
|
104
|
-
if running?
|
|
105
|
-
return
|
|
106
|
-
end
|
|
107
|
-
rescue ContainerCannotOpenError => e
|
|
108
|
-
last_rescued_error = e
|
|
35
|
+
return self if running?
|
|
109
36
|
sleep backoff
|
|
110
|
-
|
|
111
|
-
backoff *= multiplier**count
|
|
37
|
+
backoff *= multiplier
|
|
112
38
|
end
|
|
113
39
|
end
|
|
114
40
|
rescue Timeout::Error
|
|
115
|
-
raise
|
|
41
|
+
raise ContainerCannotOpenError, "Storage readiness timed out"
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
private
|
|
45
|
+
|
|
46
|
+
# Query the published loopback endpoint without environment proxies.
|
|
47
|
+
# @return [Net::HTTPResponse] completed response; connection errors propagate
|
|
48
|
+
def read_status
|
|
49
|
+
http = Net::HTTP.new("127.0.0.1", 4443, nil)
|
|
50
|
+
http.open_timeout = 2
|
|
51
|
+
http.read_timeout = 2
|
|
52
|
+
http.get("/storage/v1/b")
|
|
116
53
|
end
|
|
117
54
|
end
|
|
118
55
|
end
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
require "google/cloud/secret_manager/v1"
|
|
2
|
+
require "grpc"
|
|
3
|
+
require "logger"
|
|
4
|
+
|
|
5
|
+
module Bindan
|
|
6
|
+
module Emulator
|
|
7
|
+
#
|
|
8
|
+
# Checks readiness and clears local test data without managing containers.
|
|
9
|
+
#
|
|
10
|
+
class SecretManagerController
|
|
11
|
+
WAIT_TIMEOUT = 60 # seconds
|
|
12
|
+
|
|
13
|
+
class Error < StandardError; end
|
|
14
|
+
|
|
15
|
+
attr_reader :project_id, :client
|
|
16
|
+
|
|
17
|
+
#
|
|
18
|
+
# Connect to the local emulator without authentication or payload logging.
|
|
19
|
+
#
|
|
20
|
+
# @param [String] project_id project whose test secrets may be deleted
|
|
21
|
+
#
|
|
22
|
+
def initialize(project_id: "bindan-test")
|
|
23
|
+
@project_id = project_id
|
|
24
|
+
@channel = GRPC::Core::Channel.new(endpoint, {}, :this_channel_is_insecure)
|
|
25
|
+
# @type var retry_codes: Array[Integer]
|
|
26
|
+
retry_codes = []
|
|
27
|
+
@client = Google::Cloud::SecretManager::V1::SecretManagerService::Client.new do |config|
|
|
28
|
+
config.endpoint = endpoint
|
|
29
|
+
config.credentials = @channel
|
|
30
|
+
config.logger = Logger.new(nil)
|
|
31
|
+
config.timeout = 2
|
|
32
|
+
config.retry_policy = {retry_codes: retry_codes}
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
#
|
|
37
|
+
# @return [String] loopback SDK endpoint (host:port, without an HTTP scheme)
|
|
38
|
+
#
|
|
39
|
+
def endpoint
|
|
40
|
+
"127.0.0.1:9090"
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
#
|
|
44
|
+
# Wait for an SDK list request to succeed, retrying transient failures.
|
|
45
|
+
#
|
|
46
|
+
# @param [Numeric] timeout readiness deadline in seconds
|
|
47
|
+
# @return [self] after a successful SDK read
|
|
48
|
+
# @raise [Error] when the readiness deadline expires between attempts
|
|
49
|
+
#
|
|
50
|
+
def wait_available(timeout: WAIT_TIMEOUT)
|
|
51
|
+
deadline = Process.clock_gettime(Process::CLOCK_MONOTONIC) + timeout
|
|
52
|
+
loop do
|
|
53
|
+
remaining = deadline - Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
54
|
+
raise Error, "Secret Manager emulator readiness timed out" unless remaining > 0
|
|
55
|
+
return self if sdk_ready?([remaining, 2].min)
|
|
56
|
+
remaining = deadline - Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
57
|
+
sleep [0.1, remaining].min if remaining > 0
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
#
|
|
62
|
+
# Delete all secrets and their versions in the selected test project.
|
|
63
|
+
# Collect names before deleting so pagination cannot skip entries.
|
|
64
|
+
#
|
|
65
|
+
# @return [self] after all listed secrets have been deleted
|
|
66
|
+
#
|
|
67
|
+
def clear
|
|
68
|
+
client.list_secrets(parent: "projects/#{project_id}").to_a.each do |secret|
|
|
69
|
+
client.delete_secret(name: secret.name)
|
|
70
|
+
end
|
|
71
|
+
self
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
#
|
|
75
|
+
# Release the shared gRPC channel after the integration run.
|
|
76
|
+
#
|
|
77
|
+
# @return [void]
|
|
78
|
+
#
|
|
79
|
+
def close
|
|
80
|
+
@channel.close
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
private
|
|
84
|
+
|
|
85
|
+
#
|
|
86
|
+
# Probe the list API; only unavailable/deadline errors become false.
|
|
87
|
+
#
|
|
88
|
+
# @param [Numeric] timeout per-request timeout in seconds
|
|
89
|
+
# @return [Boolean] true on success, false on a transient startup failure
|
|
90
|
+
#
|
|
91
|
+
def sdk_ready?(timeout)
|
|
92
|
+
# @type var retry_codes: Array[Integer]
|
|
93
|
+
retry_codes = []
|
|
94
|
+
client.list_secrets(
|
|
95
|
+
{parent: "projects/#{project_id}", page_size: 1},
|
|
96
|
+
{timeout: timeout, retry_policy: {retry_codes: retry_codes}}
|
|
97
|
+
).each { |_| }
|
|
98
|
+
true
|
|
99
|
+
rescue Google::Cloud::UnavailableError, Google::Cloud::DeadlineExceededError
|
|
100
|
+
false
|
|
101
|
+
end
|
|
102
|
+
end
|
|
103
|
+
end
|
|
104
|
+
end
|
|
@@ -33,7 +33,20 @@ module Bindan
|
|
|
33
33
|
class ColOrDocNotExist < Error; end
|
|
34
34
|
|
|
35
35
|
#
|
|
36
|
-
#
|
|
36
|
+
# 1. Supply collection when registering the provider to avoid repeating it
|
|
37
|
+
# on each document lookup.
|
|
38
|
+
# 2. Omit collection to access multiple collections through one provider.
|
|
39
|
+
# Each lookup must then explicitly supply a full document path
|
|
40
|
+
# (collection/document).
|
|
41
|
+
#
|
|
42
|
+
# Path examples (collection, key -> document path):
|
|
43
|
+
# - "app-settings", "flags" -> "app-settings/flags"
|
|
44
|
+
# - omitted, "app-settings/flags" -> "app-settings/flags"
|
|
45
|
+
# - omitted, "maintenance/status" -> "maintenance/status"
|
|
46
|
+
# - "app-settings", "other/flags" -> "app-settings/other/flags" (invalid)
|
|
47
|
+
# - omitted, "flags" -> "/flags" (invalid)
|
|
48
|
+
#
|
|
49
|
+
# @param [String, nil] collection fixed collection path, or nil for full-path keys
|
|
37
50
|
# @param [bool] raise_error
|
|
38
51
|
# @param [Google::Cloud::Firestore] sdk
|
|
39
52
|
#
|
|
@@ -61,19 +74,25 @@ module Bindan
|
|
|
61
74
|
@_firestore.doc(path).create(doc)
|
|
62
75
|
end
|
|
63
76
|
|
|
77
|
+
#
|
|
78
|
+
# @param [String] key document ID/path relative to collection, or full path if omitted
|
|
79
|
+
# @return [String]
|
|
80
|
+
#
|
|
81
|
+
def document_path(key)
|
|
82
|
+
if @collection.nil? && key.include?("/")
|
|
83
|
+
key
|
|
84
|
+
else
|
|
85
|
+
[@collection, key].join("/")
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
|
|
64
89
|
#
|
|
65
90
|
# @raise ColOrDocNotExist
|
|
66
|
-
# @param [String] key
|
|
91
|
+
# @param [String] key document ID/path relative to collection, or full path if omitted
|
|
67
92
|
# @return [Hash]
|
|
68
93
|
#
|
|
69
94
|
def [](key)
|
|
70
|
-
doc_path =
|
|
71
|
-
if @collection.nil? && key.include?("/")
|
|
72
|
-
key
|
|
73
|
-
else
|
|
74
|
-
[@collection, key].join("/")
|
|
75
|
-
end
|
|
76
|
-
|
|
95
|
+
doc_path = document_path(key)
|
|
77
96
|
doc = @_firestore.doc(doc_path).get
|
|
78
97
|
|
|
79
98
|
if @_options[:raise_error] && !doc.data
|
|
@@ -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
|
|
@@ -31,9 +31,24 @@ module Bindan
|
|
|
31
31
|
class FileNotExist < Error; end
|
|
32
32
|
|
|
33
33
|
#
|
|
34
|
-
#
|
|
35
|
-
#
|
|
36
|
-
#
|
|
34
|
+
# Build the bucket name as [project_id, bucket].join(separator). Keeping
|
|
35
|
+
# the same bucket suffix across projects makes staging environments easier
|
|
36
|
+
# to set up: changing project_id selects the corresponding environment's
|
|
37
|
+
# bucket without changing the suffix or the configuration lookup code.
|
|
38
|
+
#
|
|
39
|
+
# Naming examples (bucket argument: "config-bucket"):
|
|
40
|
+
# - project_id: "my-project", default separator -> "my-project-config-bucket"
|
|
41
|
+
# - project_id: "staging", default separator -> "staging-config-bucket"
|
|
42
|
+
# - project_id omitted, default separator -> "-config-bucket"
|
|
43
|
+
# - project_id omitted, separator: "" -> "config-bucket"
|
|
44
|
+
# - project_id: "my-project", separator: "" -> "my-projectconfig-bucket"
|
|
45
|
+
#
|
|
46
|
+
# Only an explicitly supplied project_id is used in the bucket name;
|
|
47
|
+
# a project ID automatically resolved by the SDK is not used.
|
|
48
|
+
#
|
|
49
|
+
# @param [String] bucket bucket name suffix, not the full bucket name
|
|
50
|
+
# @param [String] project_id project ID used for both the SDK and name prefix
|
|
51
|
+
# @param [String] separator delimiter between project_id and bucket (default: "-")
|
|
37
52
|
# @param [String] credentials
|
|
38
53
|
# @param [Google::Cloud::Storage] sdk
|
|
39
54
|
#
|
data/lib/bindan/version.rb
CHANGED
|
@@ -1,76 +1,47 @@
|
|
|
1
1
|
module Bindan
|
|
2
2
|
module Emulator
|
|
3
3
|
#
|
|
4
|
-
# Represents and controls a single instance of the Google Cloud Firestore emulator
|
|
5
|
-
# This class
|
|
6
|
-
# separating process management from the test execution flow.
|
|
4
|
+
# Represents and controls a single instance of the Google Cloud Firestore emulator service.
|
|
5
|
+
# This class checks readiness and clears test data without managing containers.
|
|
7
6
|
#
|
|
8
7
|
class FirestoreController
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
@host: untyped
|
|
12
|
-
|
|
13
|
-
@port: untyped
|
|
14
|
-
|
|
15
|
-
WAIT_TIMEOUT: 15
|
|
8
|
+
WAIT_TIMEOUT: 60
|
|
16
9
|
|
|
17
10
|
INITIAL_BACKOFF_KEY_SEC: ::Float
|
|
18
11
|
|
|
19
12
|
BACKOFF_MULTIPLIER: ::Float
|
|
20
13
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
#
|
|
24
|
-
def self.host_and_port: (?untyped? host, ?untyped? port) -> ::Array[untyped]
|
|
25
|
-
|
|
26
|
-
#
|
|
27
|
-
# Initiates the emulator process and returns a new instance.
|
|
28
|
-
#
|
|
29
|
-
# @param [String] import
|
|
30
|
-
# @param [String] export
|
|
31
|
-
# @param [String] host
|
|
32
|
-
# @param [Integer] port
|
|
33
|
-
# @param [bool] close_io
|
|
34
|
-
# @raise [Errno]
|
|
35
|
-
# @return [FirestoreEmulatorController] An instance to manage the emulator lifecycle.
|
|
36
|
-
#
|
|
37
|
-
def self.start: (?import: untyped?, ?export: untyped?, ?host: untyped?, ?port: untyped?, ?close_io: bool) -> untyped
|
|
14
|
+
class Error < StandardError
|
|
15
|
+
end
|
|
38
16
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
#
|
|
42
|
-
# @param [Integer] pid
|
|
43
|
-
# @param [bool] with_message
|
|
44
|
-
# @return [void]
|
|
45
|
-
#
|
|
46
|
-
def self.stop: (untyped pid, ?with_message: bool) -> untyped
|
|
17
|
+
@project_id: String
|
|
18
|
+
@readiness_error: StandardError?
|
|
47
19
|
|
|
48
|
-
|
|
49
|
-
# @param [Integer] host
|
|
50
|
-
# @param [Integer] port
|
|
51
|
-
#
|
|
52
|
-
def self.kill_process_if_already_exists: (host: untyped, port: untyped, ?with_message: bool) -> untyped
|
|
20
|
+
attr_reader project_id: String
|
|
53
21
|
|
|
54
22
|
#
|
|
55
|
-
#
|
|
23
|
+
# Select the project used by the integration tests.
|
|
56
24
|
#
|
|
57
|
-
def initialize: (
|
|
25
|
+
def initialize: (?project_id: String) -> void
|
|
58
26
|
|
|
59
27
|
#
|
|
60
|
-
#
|
|
28
|
+
# Wait for an SDK read, retrying false with bounded exponential backoff.
|
|
61
29
|
#
|
|
62
|
-
# @raise [
|
|
30
|
+
# @raise [Error] when the overall deadline expires, including during a check
|
|
63
31
|
# @param [number] timeout
|
|
64
32
|
# @param [number] backoff
|
|
65
33
|
# @param [number] multiplier
|
|
66
|
-
# @return [
|
|
34
|
+
# @return [self] after a successful SDK read
|
|
67
35
|
#
|
|
68
36
|
def wait_available: (?timeout: untyped, ?backoff: untyped, ?multiplier: untyped, ?with_message: bool) -> untyped
|
|
69
37
|
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
38
|
+
def endpoint: () -> String
|
|
39
|
+
def clear: (?timeout: Numeric) -> FirestoreController
|
|
40
|
+
|
|
41
|
+
private
|
|
42
|
+
|
|
43
|
+
def sdk_ready?: (Numeric timeout) -> bool
|
|
44
|
+
def delete_documents: (String path, Numeric timeout) -> Net::HTTPResponse
|
|
74
45
|
end
|
|
75
46
|
end
|
|
76
47
|
end
|
|
@@ -1,15 +1,9 @@
|
|
|
1
1
|
module Bindan
|
|
2
2
|
module Emulator
|
|
3
3
|
class GcsServerController
|
|
4
|
-
@container_name: untyped
|
|
5
|
-
|
|
6
|
-
@folder: untyped
|
|
7
|
-
|
|
8
4
|
class ContainerCannotOpenError < Error
|
|
9
5
|
end
|
|
10
6
|
|
|
11
|
-
CONTAINER_NAME: "gcs-server"
|
|
12
|
-
|
|
13
7
|
WAIT_TIMEOUT: 15
|
|
14
8
|
|
|
15
9
|
INITIAL_BACKOFF_KEY_SEC: ::Float
|
|
@@ -17,55 +11,24 @@ module Bindan
|
|
|
17
11
|
BACKOFF_MULTIPLIER: ::Float
|
|
18
12
|
|
|
19
13
|
#
|
|
20
|
-
# @raise [Errno]
|
|
21
|
-
# @param [String] folder
|
|
22
|
-
# @param [Integer] [port]
|
|
23
|
-
# @param [String] [name]
|
|
24
|
-
# @return [GcsServerController]
|
|
25
|
-
#
|
|
26
|
-
def self.start: (folder: untyped, ?port: ::Integer, ?name: untyped, ?close_io: bool) -> untyped
|
|
27
|
-
|
|
28
|
-
#
|
|
29
|
-
# @raise [IOError]
|
|
30
|
-
# @param [String] name
|
|
31
14
|
# @return [bool]
|
|
32
15
|
#
|
|
33
|
-
def
|
|
34
|
-
|
|
35
|
-
#
|
|
36
|
-
# @param [String] name
|
|
37
|
-
# @raise [Errno]
|
|
38
|
-
# @return [void]
|
|
39
|
-
#
|
|
40
|
-
def self.stop: (name: untyped) -> untyped
|
|
41
|
-
|
|
42
|
-
#
|
|
43
|
-
# @param [String] name
|
|
44
|
-
# @param [String] folder
|
|
45
|
-
#
|
|
46
|
-
def initialize: (name: untyped, folder: untyped) -> void
|
|
47
|
-
|
|
48
|
-
#
|
|
49
|
-
# @return
|
|
50
|
-
#
|
|
51
|
-
def stop: () -> untyped
|
|
52
|
-
|
|
53
|
-
#
|
|
54
|
-
# @param [String] name
|
|
55
|
-
# @return [bool]
|
|
56
|
-
#
|
|
57
|
-
def running?: () -> untyped
|
|
16
|
+
def running?: () -> bool
|
|
58
17
|
|
|
59
18
|
#
|
|
60
19
|
# waits container to be running
|
|
61
20
|
#
|
|
62
|
-
# @raise [
|
|
21
|
+
# @raise [ContainerCannotOpenError] if the container does not become available within the configured timeout.
|
|
63
22
|
# @param [number] timeout
|
|
64
23
|
# @param [number] backoff
|
|
65
24
|
# @param [number] multiplier
|
|
66
|
-
# @return [
|
|
25
|
+
# @return [self]
|
|
67
26
|
#
|
|
68
|
-
def wait_available: (?timeout:
|
|
27
|
+
def wait_available: (?timeout: Numeric, ?backoff: Numeric, ?multiplier: Numeric) -> GcsServerController
|
|
28
|
+
|
|
29
|
+
private
|
|
30
|
+
|
|
31
|
+
def read_status: () -> Net::HTTPResponse
|
|
69
32
|
end
|
|
70
33
|
end
|
|
71
34
|
end
|
|
@@ -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 < StandardError
|
|
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
|
metadata
CHANGED
|
@@ -1,20 +1,20 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: bindan
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.2.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- wtnabe
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: exe
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date:
|
|
11
|
+
date: 2026-11-14 00:00:00.000000000 Z
|
|
12
12
|
dependencies: []
|
|
13
|
-
description: Bindan is a Ruby gem for building single configuration object from
|
|
14
|
-
sources with providers (
|
|
15
|
-
|
|
16
|
-
lazy initialization and seamless switching between development
|
|
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
20
|
executables: []
|
|
@@ -30,13 +30,16 @@ files:
|
|
|
30
30
|
- Rakefile
|
|
31
31
|
- Steepfile
|
|
32
32
|
- compose.yml
|
|
33
|
+
- docs/secret-manager-operations.md
|
|
33
34
|
- lib/bindan.rb
|
|
34
35
|
- lib/bindan/emulators/firestore_controller.rb
|
|
35
36
|
- lib/bindan/emulators/gcs_server_controller.rb
|
|
37
|
+
- lib/bindan/emulators/secret_manager_controller.rb
|
|
36
38
|
- lib/bindan/error.rb
|
|
37
39
|
- lib/bindan/providers.rb
|
|
38
40
|
- lib/bindan/providers/envvar.rb
|
|
39
41
|
- lib/bindan/providers/firestore.rb
|
|
42
|
+
- lib/bindan/providers/secret_manager.rb
|
|
40
43
|
- lib/bindan/providers/storage.rb
|
|
41
44
|
- lib/bindan/version.rb
|
|
42
45
|
- rbs_collection.lock.yaml
|
|
@@ -44,9 +47,11 @@ files:
|
|
|
44
47
|
- sig/bindan.rbs
|
|
45
48
|
- sig/bindan/emulators/firestore_controller.rbs
|
|
46
49
|
- sig/bindan/emulators/gcs_server_controller.rbs
|
|
50
|
+
- sig/bindan/emulators/secret_manager_controller.rbs
|
|
47
51
|
- sig/bindan/error.rbs
|
|
48
52
|
- sig/bindan/providers/envvar.rbs
|
|
49
53
|
- sig/bindan/providers/firestore.rbs
|
|
54
|
+
- sig/bindan/providers/secret_manager.rbs
|
|
50
55
|
- sig/bindan/providers/storage.rbs
|
|
51
56
|
- sig/bindan/version.rbs
|
|
52
57
|
homepage: https://github.com/wtnabe/bindan
|