-
Notifications
You must be signed in to change notification settings - Fork 59
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
control-service: secrets service implementation #2241
Merged
dakodakov
merged 20 commits into
main
from
person/ddakov/secrets-service-initial-implementation
Jun 14, 2023
Merged
Changes from 7 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
bd4eaa6
control-service: secrets service implementation
f11ddee
Google Java Format
d2e5db4
control-service: secrets service implementation
8483232
control-service: secrets service implementation
3ec9ed1
Google Java Format
513d798
control-service: secrets service implementation
b42bcc5
Google Java Format
185f525
control-service: secrets service implementation
5a8ad2a
control-service: secrets service implementation
7a0ea85
Google Java Format
94e01c4
control-service: secrets service implementation
5b75ce7
Google Java Format
ad08e7a
control-service: secrets service implementation
620efac
Google Java Format
3ae270e
control-service: secrets service implementation
d1b4b52
Google Java Format
a5e8b6e
control-service: secrets service implementation
4cab724
control-service: secrets service implementation
a538fa2
Google Java Format
79b66b7
Merge branch 'main' into person/ddakov/secrets-service-initial-implem…
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 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 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 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
14 changes: 14 additions & 0 deletions
14
...ervice/src/main/java/com/vmware/taurus/exception/SecretStorageNotConfiguredException.java
This file contains 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,14 @@ | ||
/* | ||
* Copyright 2021-2023 VMware, Inc. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package com.vmware.taurus.exception; | ||
|
||
/** Exception thrown, when a secret storage has not been configured */ | ||
public class SecretStorageNotConfiguredException extends ExternalSystemError { | ||
|
||
public SecretStorageNotConfiguredException() { | ||
super(MainExternalSystem.SECRETS, "Not Configured"); | ||
} | ||
} |
This file contains 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
23 changes: 23 additions & 0 deletions
23
...pipelines_control_service/src/main/java/com/vmware/taurus/secrets/service/JobSecrets.java
This file contains 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,23 @@ | ||
/* | ||
* Copyright 2021-2023 VMware, Inc. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package com.vmware.taurus.secrets.service; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
import org.springframework.data.annotation.Id; | ||
import org.springframework.vault.repository.mapping.Secret; | ||
|
||
@Data | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@Secret | ||
dakodakov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
public class JobSecrets { | ||
|
||
@Id private String jobName; | ||
|
||
private String secretsJson; | ||
} |
91 changes: 91 additions & 0 deletions
91
...es_control_service/src/main/java/com/vmware/taurus/secrets/service/JobSecretsService.java
This file contains 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,91 @@ | ||
/* | ||
* Copyright 2021-2023 VMware, Inc. | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package com.vmware.taurus.secrets.service; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.vmware.taurus.base.FeatureFlags; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.json.JSONObject; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.vault.authentication.TokenAuthentication; | ||
import org.springframework.vault.client.VaultEndpoint; | ||
import org.springframework.vault.core.VaultTemplate; | ||
import org.springframework.vault.support.Versioned; | ||
|
||
import java.net.URI; | ||
import java.net.URISyntaxException; | ||
import java.util.Collections; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
|
||
@Slf4j | ||
@Service | ||
public class JobSecretsService { | ||
|
||
private static final String SECRET = "secret"; | ||
|
||
private final ObjectMapper objectMapper = new ObjectMapper(); | ||
private VaultTemplate vaultTemplate; | ||
dakodakov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
public JobSecretsService( | ||
@Value("${vdk.vault.uri:}") String vaultUri, | ||
dakodakov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
@Value("${vdk.vault.token:}") String vaultToken, | ||
FeatureFlags featureFlags) | ||
throws URISyntaxException { | ||
|
||
if (featureFlags.isVaultIntegrationEnabled()) { | ||
dakodakov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
VaultEndpoint vaultEndpoint = VaultEndpoint.from(new URI(vaultUri)); | ||
TokenAuthentication clientAuthentication = new TokenAuthentication(vaultToken); | ||
|
||
this.vaultTemplate = new VaultTemplate(vaultEndpoint, clientAuthentication); | ||
dakodakov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
|
||
public void updateJobSecrets(String jobName, Map<String, Object> secrets) { | ||
Versioned<JobSecrets> readResponse = | ||
vaultTemplate.opsForVersionedKeyValue(SECRET).get(jobName, JobSecrets.class); | ||
|
||
JobSecrets jobSecrets; | ||
|
||
if (readResponse != null && readResponse.hasData()) { | ||
jobSecrets = readResponse.getData(); | ||
} else { | ||
jobSecrets = new JobSecrets(jobName, null); | ||
} | ||
|
||
var updatedSecrets = | ||
secrets.entrySet().stream() | ||
.collect( | ||
Collectors.toMap( | ||
Map.Entry::getKey, | ||
entry -> { | ||
if (entry.getValue() == null) { | ||
entry.setValue(JSONObject.NULL); | ||
} | ||
return entry.getValue(); | ||
})); | ||
|
||
jobSecrets.setSecretsJson(new JSONObject(updatedSecrets).toString()); | ||
|
||
vaultTemplate.opsForVersionedKeyValue(SECRET).put(jobName, jobSecrets); | ||
} | ||
|
||
public Map<String, Object> readJobSecrets(String jobName) throws JsonProcessingException { | ||
Versioned<JobSecrets> readResponse = | ||
vaultTemplate.opsForVersionedKeyValue(SECRET).get(jobName, JobSecrets.class); | ||
|
||
JobSecrets jobSecrets; | ||
|
||
if (readResponse != null && readResponse.hasData()) { | ||
jobSecrets = readResponse.getData(); | ||
return objectMapper.readValue(jobSecrets.getSecretsJson(), Map.class); | ||
} else { | ||
return Collections.emptyMap(); | ||
} | ||
} | ||
} |
This file contains 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 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
External System error is 503. That does not seem suitable here.
503 is generally a re-triable error. And often would be re-tried. But as the secrets feature is disbaled re-trying is unnecessary.
403 Forbidden (means The server understood the request, but is refusing to fulfill it.)
501 Not Implemented
seem more suitable for this case. I'd vote for 501
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense. I'll fix it in the upcoming PR for the tests.