generated from oracle/template-repo
-
Notifications
You must be signed in to change notification settings - Fork 12
OCI Vault #47
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
Merged
Merged
OCI Vault #47
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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
78 changes: 78 additions & 0 deletions
78
...oci-autoconfigure/src/main/java/com/oracle/cloud/spring/vault/VaultAutoConfiguration.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,78 @@ | ||
// Copyright (c) 2024, Oracle and/or its affiliates. | ||
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/ | ||
package com.oracle.cloud.spring.vault; | ||
|
||
|
||
import com.oracle.bmc.auth.RegionProvider; | ||
import com.oracle.bmc.secrets.Secrets; | ||
import com.oracle.bmc.secrets.SecretsClient; | ||
import com.oracle.bmc.vault.Vaults; | ||
import com.oracle.bmc.vault.VaultsClient; | ||
import com.oracle.cloud.spring.autoconfigure.core.CredentialsProvider; | ||
import org.springframework.beans.factory.annotation.Qualifier; | ||
import org.springframework.boot.autoconfigure.AutoConfiguration; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
import org.springframework.boot.context.properties.EnableConfigurationProperties; | ||
import org.springframework.cloud.context.config.annotation.RefreshScope; | ||
import org.springframework.context.annotation.Bean; | ||
|
||
import static com.oracle.cloud.spring.autoconfigure.core.CredentialsProviderAutoConfiguration.credentialsProviderQualifier; | ||
import static com.oracle.cloud.spring.autoconfigure.core.RegionProviderAutoConfiguration.regionProviderQualifier; | ||
|
||
/** | ||
* Auto-configuration for initializing the OCI Vault component. | ||
* Depends on {@link com.oracle.cloud.spring.autoconfigure.core.CredentialsProviderAutoConfiguration} and | ||
* {@link com.oracle.cloud.spring.autoconfigure.core.RegionProviderAutoConfiguration} | ||
* for loading the Authentication configuration | ||
* | ||
* @see Vault | ||
*/ | ||
@AutoConfiguration | ||
@ConditionalOnClass({Vault.class}) | ||
@EnableConfigurationProperties(VaultProperties.class) | ||
@ConditionalOnProperty(name = "spring.cloud.oci.vault.enabled", havingValue = "true", matchIfMissing = true) | ||
public class VaultAutoConfiguration { | ||
private final VaultProperties properties; | ||
|
||
public VaultAutoConfiguration(VaultProperties properties) { | ||
this.properties = properties; | ||
} | ||
|
||
@Bean | ||
@RefreshScope | ||
@ConditionalOnMissingBean(Vault.class) | ||
public Vault vault(Vaults vaults, Secrets secrets) { | ||
return new VaultImpl(vaults, secrets, properties.getVaultId(), properties.getCompartment()); | ||
} | ||
|
||
@Bean | ||
@RefreshScope | ||
@ConditionalOnMissingBean | ||
public Vaults vaults(@Qualifier(regionProviderQualifier) RegionProvider regionProvider, | ||
@Qualifier(credentialsProviderQualifier) | ||
CredentialsProvider cp) { | ||
Vaults vaults = VaultsClient.builder() | ||
.build(cp.getAuthenticationDetailsProvider()); | ||
if (regionProvider.getRegion() != null) { | ||
vaults.setRegion(regionProvider.getRegion()); | ||
} | ||
return vaults; | ||
} | ||
|
||
@Bean | ||
@RefreshScope | ||
@ConditionalOnMissingBean | ||
public Secrets secrets(@Qualifier(regionProviderQualifier) RegionProvider regionProvider, | ||
@Qualifier(credentialsProviderQualifier) | ||
CredentialsProvider cp) { | ||
Secrets secrets = SecretsClient.builder() | ||
.build(cp.getAuthenticationDetailsProvider()); | ||
if (regionProvider.getRegion() != null) { | ||
secrets.setRegion(regionProvider.getRegion()); | ||
} | ||
return secrets; | ||
} | ||
} | ||
|
29 changes: 29 additions & 0 deletions
29
...-cloud-oci-autoconfigure/src/main/java/com/oracle/cloud/spring/vault/VaultProperties.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,29 @@ | ||
// Copyright (c) 2024, Oracle and/or its affiliates. | ||
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/ | ||
package com.oracle.cloud.spring.vault; | ||
|
||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
|
||
@ConfigurationProperties(prefix = VaultProperties.PREFIX) | ||
public class VaultProperties { | ||
public static final String PREFIX = "spring.cloud.oci.vault"; | ||
|
||
private String compartment; | ||
private String vaultId; | ||
|
||
public String getCompartment() { | ||
return compartment; | ||
} | ||
|
||
public void setCompartment(String compartment) { | ||
this.compartment = compartment; | ||
} | ||
|
||
public String getVaultId() { | ||
return vaultId; | ||
} | ||
|
||
public void setVaultId(String vaultId) { | ||
this.vaultId = vaultId; | ||
} | ||
} |
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
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
55 changes: 55 additions & 0 deletions
55
spring-cloud-oci-samples/spring-cloud-oci-vault-sample/pom.xml
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,55 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
Copyright (c) 2024, Oracle and/or its affiliates. | ||
Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/ | ||
--> | ||
|
||
<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"> | ||
<parent> | ||
<groupId>com.oracle.cloud.spring</groupId> | ||
<artifactId>spring-cloud-oci-samples</artifactId> | ||
<version>1.1.0-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
<groupId>com.oracle.cloud.spring.sample.genai</groupId> | ||
<artifactId>spring-cloud-oci-vault-sample</artifactId> | ||
<name>spring-cloud-oci-vault-sample</name> | ||
<description>spring-cloud-oci-vault-sample</description> | ||
<licenses> | ||
<license> | ||
<name>The Universal Permissive License (UPL), Version 1.0</name> | ||
<url>https://oss.oracle.com/licenses/upl/</url> | ||
<distribution>repo</distribution> | ||
</license> | ||
</licenses> | ||
<properties> | ||
<java.version>17</java.version> | ||
</properties> | ||
<dependencies> | ||
<dependency> | ||
<groupId>com.oracle.cloud.spring</groupId> | ||
<artifactId>spring-cloud-oci-starter-vault</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.oracle.cloud.spring.sample.common</groupId> | ||
<artifactId>spring-cloud-oci-common-samples-utils</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.oracle.cloud.spring.sample.common</groupId> | ||
<artifactId>spring-cloud-oci-common-samples-utils</artifactId> | ||
<type>test-jar</type> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-maven-plugin</artifactId> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
|
||
|
||
</project> |
13 changes: 13 additions & 0 deletions
13
...d/spring/sample/vault/springcloudocivaultsample/SpringCloudOciVaultSampleApplication.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,13 @@ | ||
// Copyright (c) 2024, Oracle and/or its affiliates. | ||
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/ | ||
package com.oracle.cloud.spring.sample.vault.springcloudocivaultsample; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
@SpringBootApplication | ||
public class SpringCloudOciVaultSampleApplication { | ||
public static void main(String[] args) { | ||
SpringApplication.run(SpringCloudOciVaultSampleApplication.class, args); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
.../java/com/oracle/cloud/spring/sample/vault/springcloudocivaultsample/VaultController.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,29 @@ | ||
// Copyright (c) 2024, Oracle and/or its affiliates. | ||
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/ | ||
package com.oracle.cloud.spring.sample.vault.springcloudocivaultsample; | ||
|
||
import com.oracle.bmc.secrets.responses.GetSecretBundleByNameResponse; | ||
import com.oracle.cloud.spring.vault.Vault; | ||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequestMapping("/demoapp/api/vault/") | ||
@Tag(name = "streaming APIs") | ||
public class VaultController { | ||
private final Vault vault; | ||
|
||
public VaultController(Vault vault) { | ||
this.vault = vault; | ||
} | ||
|
||
@GetMapping("secret") | ||
public ResponseEntity<?> getSecret(@RequestParam String secretName) { | ||
GetSecretBundleByNameResponse secret = vault.getSecret(secretName); | ||
return ResponseEntity.ok(vault.decodeBundle(secret)); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
...cloud-oci-samples/spring-cloud-oci-vault-sample/src/main/resources/application.properties
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,9 @@ | ||
# Copyright (c) 2024, Oracle and/or its affiliates. | ||
# Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/ | ||
|
||
spring.cloud.oci.region.static=us-chicago-1 | ||
spring.cloud.oci.config.type=file | ||
|
||
spring.cloud.oci.vault.compartment=${OCI_COMPARTMENT_ID} | ||
spring.cloud.oci.vault.vault-id=${OCI_VAULT_ID} | ||
spring.cloud.oci.vault.enabled=true | ||
anders-swanson marked this conversation as resolved.
Show resolved
Hide resolved
|
60 changes: 60 additions & 0 deletions
60
...src/test/java/com/oracle/cloud/spring/sample/vault/springcloudocivaultsample/VaultIT.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,60 @@ | ||
// Copyright (c) 2024, Oracle and/or its affiliates. | ||
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl/ | ||
package com.oracle.cloud.spring.sample.vault.springcloudocivaultsample; | ||
|
||
import java.util.Base64; | ||
import java.util.List; | ||
import java.util.UUID; | ||
|
||
import com.oracle.bmc.secrets.responses.GetSecretBundleByNameResponse; | ||
import com.oracle.bmc.vault.model.Base64SecretContentDetails; | ||
import com.oracle.bmc.vault.model.SecretSummary; | ||
import com.oracle.bmc.vault.model.UpdateSecretDetails; | ||
import com.oracle.bmc.vault.responses.UpdateSecretResponse; | ||
import com.oracle.cloud.spring.vault.Vault; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
/** | ||
* Requires an existing vault, identified by the OCI_VAULT_ID environment variable. | ||
*/ | ||
@SpringBootTest | ||
@EnabledIfEnvironmentVariable(named = "OCI_COMPARTMENT_ID", matches = ".+") | ||
@EnabledIfEnvironmentVariable(named = "OCI_VAULT_ID", matches = ".+") | ||
public class VaultIT { | ||
@Autowired | ||
Vault vault; | ||
|
||
private final String secretName = "mysecret"; | ||
|
||
@Test | ||
void getSecret() { | ||
GetSecretBundleByNameResponse secret = vault.getSecret(secretName); | ||
String decoded = vault.decodeBundle(secret); | ||
assertThat(decoded).isNotNull(); | ||
assertThat(decoded).hasSizeGreaterThan(1); | ||
} | ||
|
||
@Test | ||
void updateSecret() { | ||
String content = UUID.randomUUID().toString(); | ||
Base64SecretContentDetails contentDetails = Base64SecretContentDetails.builder() | ||
.content(Base64.getEncoder().encodeToString(content.getBytes())) | ||
.name(content) | ||
.build(); | ||
UpdateSecretResponse response = vault.updateSecret(secretName, UpdateSecretDetails.builder() | ||
.secretContent(contentDetails) | ||
.build()); | ||
assertThat(response.getSecret()).isNotNull(); | ||
} | ||
|
||
@Test | ||
void listSecret() { | ||
List<SecretSummary> summaries = vault.listSecrets(); | ||
assertThat(summaries).hasSize(1); | ||
} | ||
} |
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
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.