Skip to content

Commit 6872c90

Browse files
blink38ltamaster
authored andcommitted
feat: retrieve git password from key storage
1 parent 92b2da7 commit 6872c90

3 files changed

Lines changed: 85 additions & 4 deletions

File tree

src/main/groovy/com/rundeck/plugin/GitResourceModel.groovy

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ import com.dtolabs.rundeck.core.resources.format.ResourceFormatParser
1010
import com.dtolabs.rundeck.core.resources.format.ResourceFormatParserException
1111
import com.dtolabs.rundeck.core.resources.format.UnsupportedFormatException
1212
import com.dtolabs.utils.Streams
13+
import com.dtolabs.rundeck.core.execution.ExecutionContext;
14+
import com.dtolabs.rundeck.core.execution.ExecutionContextImpl;
15+
import com.rundeck.plugin.util.GitPluginUtil
16+
import org.rundeck.app.spi.Services;
17+
import com.dtolabs.rundeck.core.storage.keys.KeyStorageTree;
18+
import com.dtolabs.rundeck.core.execution.ExecutionListener
1319

1420

1521
/**
@@ -31,6 +37,42 @@ class GitResourceModel implements ResourceModelSource , WriteableModelSource{
3137
this.writable=true;
3238
}
3339

40+
GitResourceModel(Services services, Properties configuration, Framework framework) {
41+
42+
this.configuration = configuration
43+
this.framework = framework
44+
45+
this.extension=configuration.getProperty(GitResourceModelFactory.GIT_FORMAT_FILE)
46+
this.writable=Boolean.valueOf(configuration.getProperty(GitResourceModelFactory.WRITABLE))
47+
this.fileName=configuration.getProperty(GitResourceModelFactory.GIT_FILE)
48+
this.localPath=configuration.getProperty(GitResourceModelFactory.GIT_BASE_DIRECTORY)
49+
50+
if(gitManager==null){
51+
gitManager = new GitManager(configuration)
52+
}
53+
54+
ExecutionContext context = null;
55+
56+
if(services!=null){
57+
context = new ExecutionContextImpl.Builder()
58+
.framework(framework)
59+
.storageTree(services.getService(KeyStorageTree.class))
60+
.build();
61+
}else{
62+
context = new ExecutionContextImpl.Builder()
63+
.framework(framework)
64+
.build();
65+
}
66+
67+
if(configuration.getProperty(GitResourceModelFactory.GIT_PASSWORD_STORAGE)){
68+
def password = GitPluginUtil.getFromKeyStorage(configuration.getProperty(GitResourceModelFactory.GIT_PASSWORD_STORAGE), context)
69+
gitManager.setGitPassword(password)
70+
}
71+
72+
if(configuration.getProperty(GitResourceModelFactory.GIT_KEY_STORAGE)) {
73+
gitManager.setSshPrivateKeyPath(configuration.getProperty(GitResourceModelFactory.GIT_KEY_STORAGE))
74+
}
75+
}
3476

3577
GitResourceModel(Properties configuration, Framework framework) {
3678
this.configuration = configuration
@@ -45,10 +87,21 @@ class GitResourceModel implements ResourceModelSource , WriteableModelSource{
4587
gitManager = new GitManager(configuration)
4688
}
4789

48-
if(configuration.getProperty(GitResourceModelFactory.GIT_PASSWORD_STORAGE)) {
49-
gitManager.setGitPassword(configuration.getProperty(GitResourceModelFactory.GIT_PASSWORD_STORAGE))
90+
ExecutionContext context = new ExecutionContextImpl.Builder()
91+
.framework(this.framework)
92+
.storageTree(services.getService(KeyStorageTree.class))
93+
.build();
94+
95+
96+
if(configuration.getProperty(GitResourceModelFactory.GIT_PASSWORD_STORAGE)){
97+
def password = GitPluginUtil.getFromKeyStorage(configuration.getProperty(GitResourceModelFactory.GIT_PASSWORD_STORAGE), context)
98+
gitManager.setGitPassword(password)
5099
}
51100

101+
// if(configuration.getProperty(GitResourceModelFactory.GIT_PASSWORD_STORAGE)) {
102+
// gitManager.setGitPassword(configuration.getProperty(GitResourceModelFactory.GIT_PASSWORD_STORAGE))
103+
// }
104+
52105
if(configuration.getProperty(GitResourceModelFactory.GIT_KEY_STORAGE)) {
53106
gitManager.setSshPrivateKeyPath(configuration.getProperty(GitResourceModelFactory.GIT_KEY_STORAGE))
54107
}

src/main/groovy/com/rundeck/plugin/GitResourceModelFactory.groovy

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import com.dtolabs.rundeck.plugins.ServiceNameConstants
1212
import com.dtolabs.rundeck.plugins.descriptions.PluginDescription
1313
import com.dtolabs.rundeck.plugins.util.DescriptionBuilder
1414
import com.rundeck.plugin.util.GitPluginUtil
15+
import org.rundeck.app.spi.Services;
1516

1617
/**
1718
* Created by luistoledo on 12/18/17.
@@ -42,7 +43,7 @@ class GitResourceModelFactory implements ResourceModelSourceFactory,Describable
4243

4344

4445
final static Map<String, Object> renderingOptionsAuthentication = GitPluginUtil.getRenderOpt("Authentication",false)
45-
final static Map<String, Object> renderingOptionsAuthenticationPassword = GitPluginUtil.getRenderOpt("Authentication",false, true)
46+
final static Map<String, Object> renderingOptionsAuthenticationPassword = GitPluginUtil.getRenderOpt("Authentication",false, false, true)
4647
final static Map<String, Object> renderingOptionsConfig = GitPluginUtil.getRenderOpt("Configuration",false)
4748

4849
GitResourceModelFactory(Framework framework) {
@@ -76,7 +77,7 @@ Some examples:
7677
.property(PropertyUtil.bool(WRITABLE, "Writable",
7778
"Allow to write the remote file.",
7879
false,"false",null,renderingOptionsConfig))
79-
.property(PropertyUtil.string(GIT_PASSWORD_STORAGE, "Git Password", 'Password to authenticate remotely', false,
80+
.property(PropertyUtil.string(GIT_PASSWORD_STORAGE, "Git Password", 'Password to authenticate remotely', false,
8081
null,null,null, renderingOptionsAuthenticationPassword))
8182
.property(PropertyUtil.select(GIT_HOSTKEY_CHECKING, "SSH: Strict Host Key Checking", '''Use strict host key checking.
8283
If `yes`, require remote host SSH key is defined in the `~/.ssh/known_hosts` file, otherwise do not verify.''', false,
@@ -96,6 +97,13 @@ If `yes`, require remote host SSH key is defined in the `~/.ssh/known_hosts` fil
9697
ResourceModelSource createResourceModelSource(Properties configuration) throws ConfigurationException {
9798
final GitResourceModel resource = new GitResourceModel(configuration,framework)
9899

100+
return resource
101+
}
102+
103+
@Override
104+
ResourceModelSource createResourceModelSource(final Services services, final Properties configuration) throws ConfigurationException {
105+
final GitResourceModel resource = new GitResourceModel(services, configuration,framework)
106+
99107
return resource
100108
}
101109
}

src/main/groovy/com/rundeck/plugin/util/GitPluginUtil.groovy

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ package com.rundeck.plugin.util
33
import com.dtolabs.rundeck.core.plugins.configuration.StringRenderingConstants
44
import com.dtolabs.rundeck.core.storage.ResourceMeta
55
import com.dtolabs.rundeck.plugins.step.PluginStepContext
6+
import com.dtolabs.rundeck.core.execution.ExecutionContextImpl
7+
import com.dtolabs.rundeck.core.storage.keys.KeyStorageTree;
8+
import com.dtolabs.rundeck.core.execution.ExecutionListener
69

710
/**
811
* Created by luistoledo on 12/18/17.
@@ -41,4 +44,21 @@ class GitPluginUtil {
4144

4245
}
4346

47+
static String getFromKeyStorage(String path, ExecutionContextImpl context){
48+
KeyStorageTree storageTree = context.getStorageTree();
49+
50+
if (storageTree!=null){
51+
ResourceMeta contents = context.getStorageTree().getResource(path).getContents();
52+
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
53+
contents.writeContent(byteArrayOutputStream);
54+
String password = new String(byteArrayOutputStream.toByteArray());
55+
56+
return password;
57+
} else {
58+
ExecutionListener logger = context.getExecutionContext().getExecutionListener()
59+
logger.log(1, "storageTree is null. Cannot retrieve password");
60+
return null
61+
}
62+
63+
}
4464
}

0 commit comments

Comments
 (0)