-
Notifications
You must be signed in to change notification settings - Fork 74
feat: add GCP Secret Manager OpenFeature provider #1772
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mahpatil
wants to merge
7
commits into
open-feature:main
Choose a base branch
from
mahpatil:feat/gcp-secret-manager
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
da2fcaa
feat: add GCP Secret Manager OpenFeature provider
mahpatil b828b44
fix: simplify and strengthen FlagCacheCTest concurrent expiry test
mahpatil 8f0cc2c
Revert "fix: simplify and strengthen FlagCacheCTest concurrent expiry…
mahpatil 1d65ba5
fix: rewrite FlagCacheCTest with correct VmLens structure
mahpatil 675e809
test: add VmLens concurrent test for get on timed-out entry with insert
mahpatil ea481a2
refactor: use @BeforeEach fixtures in concurrentExpiryAndInsertDoNotL…
mahpatil 5f3b39e
Merge branch 'main' into feat/gcp-secret-manager
mahpatil File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| # Changelog | ||
|
|
||
| ## 0.0.1 | ||
|
|
||
| ### ✨ New Features | ||
|
|
||
| * Initial release of the GCP Secret Manager OpenFeature provider. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| # GCP Secret Manager Provider | ||
|
|
||
| An OpenFeature provider that reads feature flags from [Google Cloud Secret Manager](https://cloud.google.com/secret-manager), purpose-built for secrets requiring versioning, rotation, and fine-grained IAM access control. | ||
|
|
||
| ## Installation | ||
|
|
||
| <!-- x-release-please-start-version --> | ||
| ```xml | ||
| <dependency> | ||
| <groupId>dev.openfeature.contrib.providers</groupId> | ||
| <artifactId>gcp-secret-manager</artifactId> | ||
| <version>0.0.1</version> | ||
| </dependency> | ||
| ``` | ||
| <!-- x-release-please-end-version --> | ||
|
|
||
| ## Quick Start | ||
|
|
||
| ```java | ||
| import dev.openfeature.contrib.providers.gcpsecretmanager.GcpSecretManagerProvider; | ||
| import dev.openfeature.contrib.providers.gcpsecretmanager.GcpSecretManagerProviderOptions; | ||
| import dev.openfeature.sdk.OpenFeatureAPI; | ||
|
|
||
| GcpSecretManagerProviderOptions options = GcpSecretManagerProviderOptions.builder() | ||
| .projectId("my-gcp-project") | ||
| .build(); | ||
|
|
||
| OpenFeatureAPI.getInstance().setProvider(new GcpSecretManagerProvider(options)); | ||
|
|
||
| // Evaluate a boolean flag stored as secret "enable-dark-mode" with value "true" | ||
| boolean darkMode = OpenFeatureAPI.getInstance().getClient() | ||
| .getBooleanValue("enable-dark-mode", false); | ||
| ``` | ||
|
|
||
| ## How It Works | ||
|
|
||
| Each feature flag is stored as an individual **secret** in GCP Secret Manager. The flag key maps directly to the secret name (with an optional prefix). The `latest` version is accessed by default. | ||
|
|
||
| Supported raw value formats: | ||
|
|
||
| | Flag type | Secret value example | | ||
| |-----------|---------------------| | ||
| | Boolean | `true` or `false` | | ||
| | Integer | `42` | | ||
| | Double | `3.14` | | ||
| | String | `dark-mode` | | ||
| | Object | `{"color":"blue","level":3}` | | ||
|
|
||
| ## Authentication | ||
|
|
||
| The provider uses [Application Default Credentials (ADC)](https://cloud.google.com/docs/authentication/provide-credentials-adc) by default. No explicit credentials are required when running on GCP infrastructure (Cloud Run, GKE, Compute Engine) or when `gcloud auth application-default login` has been run locally. | ||
|
|
||
| To use explicit credentials: | ||
|
|
||
| ```java | ||
| import com.google.auth.oauth2.ServiceAccountCredentials; | ||
| import java.io.FileInputStream; | ||
|
|
||
| GoogleCredentials credentials = ServiceAccountCredentials.fromStream( | ||
| new FileInputStream("/path/to/service-account-key.json")); | ||
|
|
||
| GcpSecretManagerProviderOptions options = GcpSecretManagerProviderOptions.builder() | ||
| .projectId("my-gcp-project") | ||
| .credentials(credentials) | ||
| .build(); | ||
| ``` | ||
|
|
||
| ## Configuration Options | ||
|
|
||
| | Option | Type | Default | Description | | ||
| |--------|------|---------|-------------| | ||
| | `projectId` | `String` | *(required)* | GCP project ID that owns the secrets | | ||
| | `credentials` | `GoogleCredentials` | `null` (ADC) | Explicit credentials; falls back to Application Default Credentials when null | | ||
| | `secretVersion` | `String` | `"latest"` | Secret version to access. Use `"latest"` for the current version or a numeric string (e.g. `"3"`) to pin to a specific version | | ||
| | `cacheExpiry` | `Duration` | `5 minutes` | How long fetched secret values are cached before re-fetching from GCP | | ||
| | `cacheMaxSize` | `int` | `500` | Maximum number of secret values held in the in-memory cache | | ||
| | `secretNamePrefix` | `String` | `null` | Optional prefix prepended to every flag key. E.g. prefix `"ff-"` maps flag `"my-flag"` to secret `"ff-my-flag"` | | ||
|
|
||
| ## Advanced Usage | ||
|
|
||
| ### Pinning to a specific secret version | ||
|
|
||
| ```java | ||
| GcpSecretManagerProviderOptions options = GcpSecretManagerProviderOptions.builder() | ||
| .projectId("my-gcp-project") | ||
| .secretVersion("5") // always use version 5 | ||
| .build(); | ||
| ``` | ||
|
|
||
| ### Secret name prefix | ||
|
|
||
| ```java | ||
| GcpSecretManagerProviderOptions options = GcpSecretManagerProviderOptions.builder() | ||
| .projectId("my-gcp-project") | ||
| .secretNamePrefix("feature-flags/") | ||
| .build(); | ||
| ``` | ||
|
|
||
| ### Tuning cache for high-throughput scenarios | ||
|
|
||
| Secret Manager has API quotas (10,000 access operations per minute per project). Use a longer `cacheExpiry` to stay within quota. | ||
|
|
||
| ```java | ||
| GcpSecretManagerProviderOptions options = GcpSecretManagerProviderOptions.builder() | ||
| .projectId("my-gcp-project") | ||
| .cacheExpiry(Duration.ofMinutes(10)) | ||
| .cacheMaxSize(1000) | ||
| .build(); | ||
| ``` | ||
|
|
||
| ## Running Integration Tests | ||
|
|
||
| Integration tests require real GCP credentials and pre-created test secrets. | ||
|
|
||
| 1. Configure ADC: `gcloud auth application-default login` | ||
| 2. Create test secrets in your project (see `GcpSecretManagerProviderIntegrationTest` for the required secret names and values) | ||
| 3. Run: | ||
|
|
||
| ```bash | ||
| GCP_PROJECT_ID=my-project mvn verify -pl providers/gcp-secret-manager -Dgroups=integration | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <project xmlns="http://maven.apache.org/POM/4.0.0" | ||
| xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
| xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 | ||
| https://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
| <modelVersion>4.0.0</modelVersion> | ||
| <parent> | ||
| <groupId>dev.openfeature.contrib</groupId> | ||
| <artifactId>parent</artifactId> | ||
| <version>[1.0,2.0)</version> | ||
| <relativePath>../../pom.xml</relativePath> | ||
| </parent> | ||
|
|
||
| <groupId>dev.openfeature.contrib.providers</groupId> | ||
| <artifactId>gcp-secret-manager</artifactId> | ||
| <version>0.0.1</version> <!--x-release-please-version --> | ||
|
|
||
| <properties> | ||
| <!-- "-" is not allowed in automatic module names --> | ||
| <module-name>${groupId}.gcpsecretmanager</module-name> | ||
| </properties> | ||
|
|
||
| <name>gcp-secret-manager</name> | ||
| <description>GCP Secret Manager provider for OpenFeature Java SDK</description> | ||
| <url>https://openfeature.dev</url> | ||
|
|
||
| <developers> | ||
| <developer> | ||
| <id>openfeaturebot</id> | ||
| <name>OpenFeature Bot</name> | ||
| <organization>OpenFeature</organization> | ||
| <url>https://openfeature.dev/</url> | ||
| </developer> | ||
| </developers> | ||
|
|
||
| <dependencies> | ||
| <!-- GCP Secret Manager client --> | ||
| <dependency> | ||
| <groupId>com.google.cloud</groupId> | ||
| <artifactId>google-cloud-secretmanager</artifactId> | ||
| <version>2.57.0</version> | ||
| </dependency> | ||
|
|
||
| <!-- JSON parsing for structured flag values --> | ||
| <dependency> | ||
| <groupId>com.fasterxml.jackson.core</groupId> | ||
| <artifactId>jackson-databind</artifactId> | ||
| <version>2.21.1</version> | ||
| </dependency> | ||
|
|
||
| <dependency> | ||
| <groupId>org.slf4j</groupId> | ||
| <artifactId>slf4j-api</artifactId> | ||
| <version>2.0.17</version> | ||
| </dependency> | ||
|
|
||
| <!-- test-only logging implementation --> | ||
| <dependency> | ||
| <groupId>org.apache.logging.log4j</groupId> | ||
| <artifactId>log4j-slf4j2-impl</artifactId> | ||
| <version>2.25.0</version> | ||
| <scope>test</scope> | ||
| </dependency> | ||
|
|
||
| <!-- concurrency testing --> | ||
| <dependency> | ||
| <groupId>com.vmlens</groupId> | ||
| <artifactId>api</artifactId> | ||
| <version>1.2.27</version> | ||
| <scope>test</scope> | ||
| </dependency> | ||
| </dependencies> | ||
|
|
||
| <build> | ||
| <plugins> | ||
| <plugin> | ||
| <groupId>org.apache.maven.plugins</groupId> | ||
| <artifactId>maven-surefire-plugin</artifactId> | ||
| <configuration> | ||
| <excludedGroups>integration</excludedGroups> | ||
| </configuration> | ||
| </plugin> | ||
| </plugins> | ||
| </build> | ||
|
|
||
| <profiles> | ||
| <profile> | ||
| <id>concurrency-tests</id> | ||
| <build> | ||
| <plugins> | ||
| <plugin> | ||
| <groupId>com.vmlens</groupId> | ||
| <artifactId>vmlens-maven-plugin</artifactId> | ||
| <version>1.2.27</version> | ||
| <executions> | ||
| <execution> | ||
| <id>test</id> | ||
| <goals> | ||
| <goal>test</goal> | ||
| </goals> | ||
| <configuration> | ||
| <includes> | ||
| <include>**/*CTest.java</include> | ||
| </includes> | ||
| <failIfNoTests>true</failIfNoTests> | ||
| </configuration> | ||
| </execution> | ||
| </executions> | ||
| </plugin> | ||
| </plugins> | ||
| </build> | ||
| </profile> | ||
| </profiles> | ||
| </project> |
100 changes: 100 additions & 0 deletions
100
...t-manager/src/main/java/dev/openfeature/contrib/providers/gcpsecretmanager/FlagCache.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,100 @@ | ||
| package dev.openfeature.contrib.providers.gcpsecretmanager; | ||
|
|
||
| import java.time.Clock; | ||
| import java.time.Duration; | ||
| import java.time.Instant; | ||
| import java.util.Collections; | ||
| import java.util.LinkedHashMap; | ||
| import java.util.Map; | ||
| import java.util.Optional; | ||
|
|
||
| /** | ||
| * Thread-safe TTL-based in-memory cache for flag values fetched from GCP Secret Manager. | ||
| * | ||
| * <p>Entries expire after the configured {@code ttl}. When the cache reaches {@code maxSize}, | ||
| * the entry with the earliest insertion time is evicted in O(1) via {@link LinkedHashMap}'s | ||
| * insertion-order iteration and {@code removeEldestEntry}. | ||
| */ | ||
| class FlagCache { | ||
|
|
||
| private final Map<String, CacheEntry> store; | ||
| private final Duration ttl; | ||
| private final Clock clock; | ||
|
|
||
| FlagCache(Duration ttl, int maxSize) { | ||
| this(ttl, maxSize, Clock.systemUTC()); | ||
| } | ||
|
|
||
| FlagCache(Duration ttl, int maxSize, Clock clock) { | ||
| this.ttl = ttl; | ||
| this.clock = clock; | ||
| this.store = Collections.synchronizedMap(new LinkedHashMap<String, CacheEntry>(16, 0.75f, false) { | ||
| @Override | ||
| protected boolean removeEldestEntry(Map.Entry<String, CacheEntry> eldest) { | ||
| return size() > maxSize; | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Returns the cached value for {@code key} if present and not expired. | ||
| * | ||
| * @param key the cache key | ||
| * @return an {@link Optional} containing the cached string, or empty if absent/expired | ||
| */ | ||
| Optional<String> get(String key) { | ||
| synchronized (store) { | ||
| CacheEntry entry = store.get(key); | ||
| if (entry == null) { | ||
| return Optional.empty(); | ||
| } | ||
| if (entry.isExpired()) { | ||
| store.remove(key); | ||
| return Optional.empty(); | ||
| } | ||
| return Optional.of(entry.value); | ||
| } | ||
| } | ||
|
mahpatil marked this conversation as resolved.
|
||
|
|
||
| /** | ||
| * Stores {@code value} under {@code key}. Eviction of the oldest entry (when the cache is | ||
| * full) is handled automatically by the underlying {@link LinkedHashMap}. | ||
| * | ||
| * @param key the cache key | ||
| * @param value the value to cache | ||
| */ | ||
| void put(String key, String value) { | ||
| store.put(key, new CacheEntry(value, ttl, clock)); | ||
| } | ||
|
|
||
| /** | ||
| * Removes the entry for {@code key}, forcing re-fetch on next access. | ||
| * | ||
| * @param key the cache key to invalidate | ||
| */ | ||
| void invalidate(String key) { | ||
| store.remove(key); | ||
| } | ||
|
|
||
| /** Removes all entries from the cache. */ | ||
| void clear() { | ||
| store.clear(); | ||
| } | ||
|
|
||
| private static final class CacheEntry { | ||
|
|
||
| final String value; | ||
| final Instant expiresAt; | ||
| final Clock clock; | ||
|
|
||
| CacheEntry(String value, Duration ttl, Clock clock) { | ||
| this.value = value; | ||
| this.clock = clock; | ||
| this.expiresAt = Instant.now(clock).plus(ttl); | ||
| } | ||
|
|
||
| boolean isExpired() { | ||
| return Instant.now(clock).isAfter(expiresAt); | ||
| } | ||
|
mahpatil marked this conversation as resolved.
|
||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.