-
Notifications
You must be signed in to change notification settings - Fork 5
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
Added support for AWS SecretManager #13
Open
musketyr
wants to merge
3
commits into
JustinPihony:master
Choose a base branch
from
musketyr:feature/secretsmanager
base: master
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
3 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 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
86 changes: 86 additions & 0 deletions
86
.../classmethod/aws/reboot/gradle/secretsmanager/AmazonSecretsManagerGetSecretValueTask.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,86 @@ | ||
/* | ||
* Copyright 2015-2016 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package jp.classmethod.aws.reboot.gradle.secretsmanager; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Files; | ||
|
||
import lombok.Getter; | ||
import lombok.Setter; | ||
|
||
import org.gradle.api.GradleException; | ||
import org.gradle.api.internal.ConventionTask; | ||
import org.gradle.api.tasks.Input; | ||
import org.gradle.api.tasks.OutputFile; | ||
import org.gradle.api.tasks.TaskAction; | ||
|
||
import com.amazonaws.services.secretsmanager.AWSSecretsManager; | ||
import com.amazonaws.services.secretsmanager.model.GetSecretValueRequest; | ||
import com.amazonaws.services.secretsmanager.model.GetSecretValueResult; | ||
|
||
public class AmazonSecretsManagerGetSecretValueTask extends ConventionTask { | ||
|
||
@Getter(onMethod = @__(@OutputFile)) | ||
@Setter | ||
private File destination; | ||
|
||
@Getter(onMethod = @__(@Input)) | ||
@Setter | ||
private String secretName; | ||
|
||
|
||
public AmazonSecretsManagerGetSecretValueTask() { | ||
setDescription("Retrieve secret value into a file"); | ||
setGroup("AWS"); | ||
} | ||
|
||
@TaskAction | ||
public void retrieveSecretValue() { | ||
getLogger().trace("Retrieving secrets from {} into {}", secretName, destination); | ||
|
||
File destination = getDestination(); | ||
String secretName = getSecretName(); | ||
|
||
if (secretName == null) { | ||
throw new GradleException("Must specify secret name"); | ||
} | ||
if (destination == null) { | ||
throw new GradleException("Must provide the destination file"); | ||
} | ||
|
||
AmazonSecretsManagerPluginExtension ext = getProject() | ||
.getExtensions() | ||
.getByType(AmazonSecretsManagerPluginExtension.class); | ||
|
||
AWSSecretsManager sm = ext.getClient(); | ||
|
||
GetSecretValueRequest request = new GetSecretValueRequest().withSecretId(secretName); | ||
|
||
GetSecretValueResult result = sm.getSecretValue(request); | ||
|
||
destination.getParentFile().mkdirs(); | ||
|
||
try { | ||
Files.write(destination.toPath(), result.getSecretString().getBytes(StandardCharsets.UTF_8)); | ||
getLogger().info("Secrets from {} has been written into {}", secretName, destination); | ||
} catch (IOException e) { | ||
getLogger().error("Exception writing the secrets file", e); | ||
} | ||
} | ||
|
||
} |
32 changes: 32 additions & 0 deletions
32
...main/java/jp/classmethod/aws/reboot/gradle/secretsmanager/AmazonSecretsManagerPlugin.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,32 @@ | ||
/* | ||
* Copyright 2015-2016 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package jp.classmethod.aws.reboot.gradle.secretsmanager; | ||
|
||
import org.gradle.api.Plugin; | ||
import org.gradle.api.Project; | ||
|
||
import jp.classmethod.aws.reboot.gradle.AwsPlugin; | ||
|
||
public class AmazonSecretsManagerPlugin implements Plugin<Project> { | ||
|
||
public void apply(Project project) { | ||
project.getPluginManager().apply(AwsPlugin.class); | ||
project.getExtensions().create( | ||
AmazonSecretsManagerPluginExtension.NAME, | ||
AmazonSecretsManagerPluginExtension.class, | ||
project); | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
.../jp/classmethod/aws/reboot/gradle/secretsmanager/AmazonSecretsManagerPluginExtension.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,56 @@ | ||
/* | ||
* Copyright 2015-2016 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package jp.classmethod.aws.reboot.gradle.secretsmanager; | ||
|
||
import org.gradle.api.Project; | ||
|
||
import com.amazonaws.services.secretsmanager.AWSSecretsManagerClient; | ||
import com.amazonaws.services.secretsmanager.AWSSecretsManagerClientBuilder; | ||
|
||
import jp.classmethod.aws.reboot.gradle.AwsPluginExtension; | ||
import jp.classmethod.aws.reboot.gradle.common.BaseRegionAwarePluginExtension; | ||
|
||
public class AmazonSecretsManagerPluginExtension extends BaseRegionAwarePluginExtension<AWSSecretsManagerClient> { | ||
|
||
public static final String NAME = "secretsmanager"; | ||
|
||
|
||
public AmazonSecretsManagerPluginExtension(Project project) { | ||
super(project, AWSSecretsManagerClient.class); | ||
} | ||
|
||
@Override | ||
protected AWSSecretsManagerClient initClient() { | ||
AWSSecretsManagerClientBuilder builder = AWSSecretsManagerClient.builder(); | ||
|
||
AwsPluginExtension aws = getProject().getExtensions().getByType(AwsPluginExtension.class); | ||
String profile = aws.getProfileName() == null ? System.getenv("AWS_PROFILE") : aws.getProfileName(); | ||
|
||
getProject().getLogger().info("Using profile {} for authorization", profile); | ||
|
||
builder.withCredentials(aws.newCredentialsProvider(profile)); | ||
|
||
if (getRegion() != null) { | ||
getProject().getLogger().info("Using region {} from the Secrets Manager extension", getRegion()); | ||
builder.withRegion(getRegion()); | ||
} else if (aws.getRegion() != null) { | ||
getProject().getLogger().info("Using region {} from the AWS extension", getRegion()); | ||
builder.withRegion(aws.getRegion()); | ||
} | ||
|
||
return (AWSSecretsManagerClient) builder.build(); | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
...ain/resources/META-INF/gradle-plugins/jp.classmethod.aws.reboot.secretsmanager.properties
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,17 @@ | ||
# | ||
# Copyright 2013-2016 Classmethod, Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
|
||
implementation-class=jp.classmethod.aws.reboot.gradle.secretsmanager.AmazonSecretsManagerPlugin |
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.
This should be added to the README
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.
I'm not quite sure where should I put it