Skip to content

Commit c0cf954

Browse files
committed
clean
add test
1 parent 6872c90 commit c0cf954

File tree

4 files changed

+101
-71
lines changed

4 files changed

+101
-71
lines changed

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

Lines changed: 23 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,17 @@ 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;
13+
import com.dtolabs.rundeck.core.execution.ExecutionContext
14+
import com.dtolabs.rundeck.core.execution.ExecutionContextImpl
1515
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
19-
16+
import groovy.transform.CompileStatic
17+
import org.rundeck.app.spi.Services
18+
import com.dtolabs.rundeck.core.storage.keys.KeyStorageTree
2019

2120
/**
2221
* Created by luistoledo on 12/18/17.
2322
*/
23+
@CompileStatic
2424
class GitResourceModel implements ResourceModelSource , WriteableModelSource{
2525

2626
private Properties configuration;
@@ -38,7 +38,14 @@ class GitResourceModel implements ResourceModelSource , WriteableModelSource{
3838
}
3939

4040
GitResourceModel(Services services, Properties configuration, Framework framework) {
41+
configure(configuration,framework,services)
42+
}
43+
44+
GitResourceModel(Properties configuration, Framework framework) {
45+
configure(configuration,framework, null)
46+
}
4147

48+
def configure(Properties configuration, Framework framework, Services services){
4249
this.configuration = configuration
4350
this.framework = framework
4451

@@ -51,61 +58,23 @@ class GitResourceModel implements ResourceModelSource , WriteableModelSource{
5158
gitManager = new GitManager(configuration)
5259
}
5360

54-
ExecutionContext context = null;
55-
56-
if(services!=null){
57-
context = new ExecutionContextImpl.Builder()
61+
if(services && configuration.getProperty(GitResourceModelFactory.GIT_PASSWORD_STORAGE_PATH)){
62+
ExecutionContext context = new ExecutionContextImpl.Builder()
5863
.framework(framework)
5964
.storageTree(services.getService(KeyStorageTree.class))
6065
.build();
61-
}else{
62-
context = new ExecutionContextImpl.Builder()
63-
.framework(framework)
64-
.build();
65-
}
6666

67-
if(configuration.getProperty(GitResourceModelFactory.GIT_PASSWORD_STORAGE)){
68-
def password = GitPluginUtil.getFromKeyStorage(configuration.getProperty(GitResourceModelFactory.GIT_PASSWORD_STORAGE), context)
67+
def password = GitPluginUtil.getFromKeyStorage(configuration.getProperty(GitResourceModelFactory.GIT_PASSWORD_STORAGE_PATH), context)
6968
gitManager.setGitPassword(password)
7069
}
7170

72-
if(configuration.getProperty(GitResourceModelFactory.GIT_KEY_STORAGE)) {
73-
gitManager.setSshPrivateKeyPath(configuration.getProperty(GitResourceModelFactory.GIT_KEY_STORAGE))
71+
if(configuration.getProperty(GitResourceModelFactory.GIT_PASSWORD_STORAGE)) {
72+
gitManager.setGitPassword(configuration.getProperty(GitResourceModelFactory.GIT_PASSWORD_STORAGE))
7473
}
75-
}
76-
77-
GitResourceModel(Properties configuration, Framework framework) {
78-
this.configuration = configuration
79-
this.framework = framework
80-
81-
this.extension=configuration.getProperty(GitResourceModelFactory.GIT_FORMAT_FILE)
82-
this.writable=Boolean.valueOf(configuration.getProperty(GitResourceModelFactory.WRITABLE))
83-
this.fileName=configuration.getProperty(GitResourceModelFactory.GIT_FILE)
84-
this.localPath=configuration.getProperty(GitResourceModelFactory.GIT_BASE_DIRECTORY)
85-
86-
if(gitManager==null){
87-
gitManager = new GitManager(configuration)
88-
}
89-
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)
99-
}
100-
101-
// if(configuration.getProperty(GitResourceModelFactory.GIT_PASSWORD_STORAGE)) {
102-
// gitManager.setGitPassword(configuration.getProperty(GitResourceModelFactory.GIT_PASSWORD_STORAGE))
103-
// }
10474

10575
if(configuration.getProperty(GitResourceModelFactory.GIT_KEY_STORAGE)) {
10676
gitManager.setSshPrivateKeyPath(configuration.getProperty(GitResourceModelFactory.GIT_KEY_STORAGE))
10777
}
108-
10978
}
11079

11180
@Override
@@ -133,7 +102,6 @@ class GitResourceModel implements ResourceModelSource , WriteableModelSource{
133102
throw new ResourceModelSourceException(
134103
"Error requesting Resource Model Source from GIT, " +e.getMessage(),e);
135104
}
136-
return null
137105
}
138106

139107
private ResourceFormatParser getResourceFormatParser() throws UnsupportedFormatException {
@@ -151,18 +119,18 @@ class GitResourceModel implements ResourceModelSource , WriteableModelSource{
151119
}
152120

153121
@Override
154-
public SourceType getSourceType() {
122+
SourceType getSourceType() {
155123
return writable ? SourceType.READ_WRITE : SourceType.READ_ONLY;
156124
}
157125

158126
@Override
159-
public WriteableModelSource getWriteable() {
127+
WriteableModelSource getWriteable() {
160128
return writable ? this : null;
161129
}
162130

163131

164132
@Override
165-
public String getSyntaxMimeType() {
133+
String getSyntaxMimeType() {
166134
try {
167135
return getResourceFormatParser().getPreferredMimeType();
168136
} catch (UnsupportedFormatException e) {
@@ -193,7 +161,7 @@ class GitResourceModel implements ResourceModelSource , WriteableModelSource{
193161
}
194162

195163
@Override
196-
public long writeData(InputStream data) throws IOException, ResourceModelSourceException {
164+
long writeData(InputStream data) throws IOException, ResourceModelSourceException {
197165
if (!writable) {
198166
throw new IllegalArgumentException("Cannot write to file, it is not configured to be writeable");
199167
}
@@ -225,7 +193,7 @@ class GitResourceModel implements ResourceModelSource , WriteableModelSource{
225193
}
226194

227195
@Override
228-
public String getSourceDescription() {
196+
String getSourceDescription() {
229197
String gitURL=configuration.getProperty(GitResourceModelFactory.GIT_URL)
230198
return "Git repo: "+gitURL+", file:"+this.fileName;
231199
}

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

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ import org.rundeck.app.spi.Services;
1717
/**
1818
* Created by luistoledo on 12/18/17.
1919
*/
20-
@Plugin(name = GitResourceModelFactory.PROVIDER_NAME, service = ServiceNameConstants.ResourceModelSource)
21-
@PluginDescription(title = GitResourceModelFactory.PROVIDER_TITLE, description = GitResourceModelFactory.PROVIDER_DESCRIPTION)
20+
@Plugin(name = PROVIDER_NAME, service = ServiceNameConstants.ResourceModelSource)
21+
@PluginDescription(title = PROVIDER_TITLE, description = PROVIDER_DESCRIPTION)
2222
class GitResourceModelFactory implements ResourceModelSourceFactory,Describable {
2323

2424
private Framework framework;
@@ -39,11 +39,14 @@ class GitResourceModelFactory implements ResourceModelSourceFactory,Describable
3939
public final static String GIT_HOSTKEY_CHECKING="strictHostKeyChecking"
4040
public final static String GIT_KEY_STORAGE="gitKeyPath"
4141
public final static String GIT_PASSWORD_STORAGE="gitPasswordPath"
42+
public final static String GIT_PASSWORD_STORAGE_PATH="gitPasswordPathStorage"
43+
4244
public static final String WRITABLE="writable";
4345

4446

4547
final static Map<String, Object> renderingOptionsAuthentication = GitPluginUtil.getRenderOpt("Authentication",false)
46-
final static Map<String, Object> renderingOptionsAuthenticationPassword = GitPluginUtil.getRenderOpt("Authentication",false, false, true)
48+
final static Map<String, Object> renderingOptionsAuthenticationPassword = GitPluginUtil.getRenderOpt("Authentication",false, true)
49+
final static Map<String, Object> renderingOptionsAuthenticationPasswordStorage = GitPluginUtil.getRenderOpt("Authentication",false, false, true)
4750
final static Map<String, Object> renderingOptionsConfig = GitPluginUtil.getRenderOpt("Configuration",false)
4851

4952
GitResourceModelFactory(Framework framework) {
@@ -73,37 +76,33 @@ Some examples:
7376
.property(PropertyUtil.string(GIT_FILE, "Resource model File", "Resource model file inside the github repo.", true,
7477
null,null,null, renderingOptionsConfig))
7578
.property(PropertyUtil.select(GIT_FORMAT_FILE, "File Format", 'File Format', true,
76-
"xml",GitResourceModelFactory.LIST_FILE_TYPE,null, renderingOptionsConfig))
79+
"xml", LIST_FILE_TYPE,null, renderingOptionsConfig))
7780
.property(PropertyUtil.bool(WRITABLE, "Writable",
7881
"Allow to write the remote file.",
7982
false,"false",null,renderingOptionsConfig))
80-
.property(PropertyUtil.string(GIT_PASSWORD_STORAGE, "Git Password", 'Password to authenticate remotely', false,
83+
.property(PropertyUtil.string(GIT_PASSWORD_STORAGE, "Git Password", 'Password to authenticate remotely', false,
8184
null,null,null, renderingOptionsAuthenticationPassword))
85+
.property(PropertyUtil.string(GIT_PASSWORD_STORAGE_PATH, "Git Password", 'Password Storage to authenticate remotely', false,
86+
null,null,null, renderingOptionsAuthenticationPasswordStorage))
8287
.property(PropertyUtil.select(GIT_HOSTKEY_CHECKING, "SSH: Strict Host Key Checking", '''Use strict host key checking.
8388
If `yes`, require remote host SSH key is defined in the `~/.ssh/known_hosts` file, otherwise do not verify.''', false,
84-
"yes",GitResourceModelFactory.LIST_HOSTKEY_CHECKING,null, renderingOptionsAuthentication))
89+
"yes", LIST_HOSTKEY_CHECKING,null, renderingOptionsAuthentication))
8590
.property(PropertyUtil.string(GIT_KEY_STORAGE, "SSH Key Path", 'SSH Key Path', false,
8691
null,null,null, renderingOptionsAuthentication))
8792
.build()
8893

89-
90-
9194
@Override
9295
Description getDescription() {
9396
return DESCRIPTION
9497
}
9598

9699
@Override
97100
ResourceModelSource createResourceModelSource(Properties configuration) throws ConfigurationException {
98-
final GitResourceModel resource = new GitResourceModel(configuration,framework)
99-
100-
return resource
101+
return new GitResourceModel(configuration,framework)
101102
}
102103

103104
@Override
104105
ResourceModelSource createResourceModelSource(final Services services, final Properties configuration) throws ConfigurationException {
105-
final GitResourceModel resource = new GitResourceModel(services, configuration,framework)
106-
107-
return resource
106+
return new GitResourceModel(services, configuration,framework)
108107
}
109108
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@ import com.dtolabs.rundeck.plugins.step.PluginStepContext
66
import com.dtolabs.rundeck.core.execution.ExecutionContextImpl
77
import com.dtolabs.rundeck.core.storage.keys.KeyStorageTree;
88
import com.dtolabs.rundeck.core.execution.ExecutionListener
9+
import groovy.transform.CompileStatic
910

1011
/**
1112
* Created by luistoledo on 12/18/17.
1213
*/
14+
@CompileStatic
1315
class GitPluginUtil {
1416
static Map<String, Object> getRenderOpt(String value, boolean secondary, boolean password = false, boolean storagePassword = false, boolean storageKey = false) {
1517
Map<String, Object> ret = new HashMap<>();
@@ -45,7 +47,7 @@ class GitPluginUtil {
4547
}
4648

4749
static String getFromKeyStorage(String path, ExecutionContextImpl context){
48-
KeyStorageTree storageTree = context.getStorageTree();
50+
KeyStorageTree storageTree = (KeyStorageTree)context.getStorageTree()
4951

5052
if (storageTree!=null){
5153
ResourceMeta contents = context.getStorageTree().getResource(path).getContents();
@@ -55,7 +57,7 @@ class GitPluginUtil {
5557

5658
return password;
5759
} else {
58-
ExecutionListener logger = context.getExecutionContext().getExecutionListener()
60+
ExecutionListener logger = context.getExecutionListener()
5961
logger.log(1, "storageTree is null. Cannot retrieve password");
6062
return null
6163
}

src/test/groovy/com/rundeck/plugin/GitResourceModelSpec.groovy

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ import com.dtolabs.rundeck.core.common.Framework
44
import com.dtolabs.rundeck.core.common.INodeSet
55
import com.dtolabs.rundeck.core.resources.format.ResourceFormatParser
66
import com.dtolabs.rundeck.core.resources.format.ResourceFormatParserService
7+
import com.dtolabs.rundeck.core.storage.keys.KeyStorageTree
8+
import org.rundeck.app.spi.Services
9+
import org.rundeck.storage.api.Resource
710
import spock.lang.Specification
11+
import com.dtolabs.rundeck.core.storage.ResourceMeta
812

913
/**
1014
* Created by luistoledo on 12/22/17.
@@ -160,6 +164,63 @@ class GitResourceModelSpec extends Specification{
160164
'resources' |'resources.json' | 'json'
161165
}
162166

167+
def "retrieve resource success using password authentication from key storage"() {
168+
given:
169+
170+
def nodeSet = Mock(INodeSet)
171+
def framework = getFramework(nodeSet)
172+
173+
174+
175+
String path = "resources"
176+
String fileName = "resources.xml"
177+
String format = "xml"
178+
179+
File folder = new File(path)
180+
if(!folder.exists()){
181+
folder.mkdir()
182+
}
183+
184+
Properties configuration = [
185+
gitBaseDirectory:path,
186+
gitFormatFile:format,
187+
gitFile:fileName,
188+
gitPasswordPathStorage:"gitPassword",
189+
]
190+
191+
def gitManager = Mock(GitManager)
192+
193+
def inputStream = GroovyMock(InputStream)
194+
KeyStorageTree keyStorageTree = Mock(KeyStorageTree){
195+
1 * getResource(_) >> Mock(Resource) {
196+
1* getContents() >> Mock(ResourceMeta) {
197+
writeContent(_) >> { args ->
198+
args[0].write('password'.bytes)
199+
return 6L
200+
}
201+
}
202+
}
203+
}
204+
205+
Services services = Mock(Services){
206+
1 * getService(KeyStorageTree) >> keyStorageTree
207+
}
208+
209+
when:
210+
211+
def resource = new GitResourceModel(services,configuration,framework)
212+
resource.setGitManager(gitManager)
213+
214+
def result = resource.getNodes()
215+
216+
then:
217+
1 * gitManager.getFile(path) >> inputStream
218+
result == nodeSet
219+
220+
221+
222+
}
223+
163224

164225
private Framework getFramework(INodeSet nodeSet){
165226
def resourceFormatParser = Mock(ResourceFormatParser){

0 commit comments

Comments
 (0)