7
7
import com .contrastsecurity .sdk .ContrastSDK ;
8
8
import com .contrastsecurity .sdk .UserAgentProduct ;
9
9
import java .io .IOException ;
10
+ import java .nio .file .FileAlreadyExistsException ;
10
11
import java .nio .file .Files ;
11
12
import java .nio .file .Path ;
12
13
import java .nio .file .Paths ;
13
14
import java .nio .file .StandardOpenOption ;
14
15
import java .text .SimpleDateFormat ;
15
16
import java .util .Collection ;
16
- import java .util .Collections ;
17
17
import java .util .Date ;
18
+ import java .util .HashSet ;
19
+ import java .util .Objects ;
18
20
import org .gradle .api .DefaultTask ;
19
- import org .gradle .api .tasks . JavaExec ;
21
+ import org .gradle .api .Project ;
20
22
import org .gradle .api .tasks .TaskAction ;
21
- import org .gradle .internal .impldep .org .apache .commons .io .FileUtils ;
23
+ import org .gradle .api .tasks .testing .Test ;
24
+ import org .jetbrains .annotations .VisibleForTesting ;
22
25
23
26
/**
24
27
* Downloads the current java agent from TeamServer using Credentials provided by the
@@ -31,25 +34,48 @@ public class InstallAgentTask extends DefaultTask {
31
34
32
35
@ TaskAction
33
36
void installAgent () {
37
+ System .out .println ("Running installAgent task" );
34
38
35
39
// create sdk object for connecting to Contrast
36
40
final ContrastSDK sdk = connectToContrast ();
41
+ System .out .println ("Connected to Contrast at: " + sdk .getRestApiURL ());
37
42
38
43
// get agent, either from configured jar path or from TS
39
- final Path agent = retrieveAgent (sdk );
44
+ final Path agent = retrieveAgent (sdk , config . getJarPath (), config . getOrgUuid (), getProject () );
40
45
46
+ System .out .println ("preparing to attach agent" );
41
47
attachAgentToTasks (agent .toAbsolutePath ());
42
48
}
43
49
44
- /** Configures JavaExec tasks to run with the agent attached */
50
+ /**
51
+ * Configures tasks to run with the agent attached Should be configurable via the plugin
52
+ * configurations to determine which tasks we attach to. For now, this configuration is just a
53
+ * boolean and only attaches to Test tasks.
54
+ */
45
55
private void attachAgentToTasks (final Path agentPath ) {
46
- getProject ()
47
- .getTasks ()
48
- .withType (JavaExec .class )
49
- .configureEach (
50
- task -> {
51
- task .jvmArgs (createContrastArgs (agentPath ));
52
- });
56
+ if (config .getAttachToTests ()) {
57
+ getProject ()
58
+ .getTasks ()
59
+ .withType (Test .class )
60
+ .configureEach (
61
+ task -> {
62
+ System .out .println ("Attaching agent arguments to Test Tasks" );
63
+ task .jvmArgs (
64
+ createContrastArgs (
65
+ agentPath ,
66
+ config .getAppName (),
67
+ config .getServerName (),
68
+ config .getAppVersion ()));
69
+ System .out .println ("ADDED ARGS " + task .getName () + ":" );
70
+ Objects .requireNonNull (task .getAllJvmArgs ()).forEach (System .out ::println );
71
+ System .out .println ("-----------------------------\n " );
72
+ });
73
+
74
+ getProject ()
75
+ .getTasks ()
76
+ .withType (Test .class )
77
+ .forEach (s -> System .out .println (s .getAllJvmArgs ()));
78
+ }
53
79
}
54
80
55
81
/**
@@ -58,16 +84,17 @@ private void attachAgentToTasks(final Path agentPath) {
58
84
* @param agentPath preconfigured path to an agent defined by the ContrastConfigurationExtension
59
85
* @return Set of arguments
60
86
*/
61
- private Collection <String > createContrastArgs (final Path agentPath ) {
62
- final Collection <String > args = Collections .emptySet ();
87
+ @ VisibleForTesting
88
+ public static Collection <String > createContrastArgs (
89
+ final Path agentPath , final String appName , final String serverName , String appVersion ) {
90
+ final Collection <String > args = new HashSet <String >();
63
91
args .add ("-javaagent:" + agentPath .toAbsolutePath ());
64
- args .add ("-Dcontrast.override.appname=" + config . appName );
65
- args .add ("-Dcontrast.server=" + config . serverName );
92
+ args .add ("-Dcontrast.override.appname=" + appName );
93
+ args .add ("-Dcontrast.server=" + serverName );
66
94
args .add ("-Dcontrast.env=qa" );
67
95
68
- String appVersion = config .getAppVersion ();
69
96
if (appVersion == null ) {
70
- appVersion = computeAppVersion ();
97
+ appVersion = computeAppVersion (appName );
71
98
}
72
99
73
100
args .add ("-Dcontrast.override.appversion=" + appVersion );
@@ -81,7 +108,8 @@ private Collection<String> createContrastArgs(final Path agentPath) {
81
108
*
82
109
* @return computed AppVersion
83
110
*/
84
- private String computeAppVersion () {
111
+ @ VisibleForTesting
112
+ public static String computeAppVersion (final String appName ) {
85
113
final Date currentDate = new Date ();
86
114
String travisBuildNumber = System .getenv ("TRAVIS_BUILD_NUMBER" );
87
115
String circleBuildNum = System .getenv ("CIRCLE_BUILD_NUM" );
@@ -94,25 +122,40 @@ private String computeAppVersion() {
94
122
} else {
95
123
appVersionQualifier = new SimpleDateFormat ("yyyyMMddHHmmss" ).format (currentDate );
96
124
}
97
- return config . getAppName () + "-" + appVersionQualifier ;
125
+ return appName + "-" + appVersionQualifier ;
98
126
}
99
127
100
128
/** Use ContrastSDK to download agent and return the path where agent jar is stored */
101
- private Path retrieveAgent (final ContrastSDK connection ) {
129
+ @ VisibleForTesting
130
+ public static Path retrieveAgent (
131
+ final ContrastSDK connection ,
132
+ final String jarPath ,
133
+ final String uuid ,
134
+ final Project project ) {
102
135
// Initially attempt to run agent from the previously configured location
103
- final String jarPath = config .getJarPath ();
104
136
if (jarPath != null ) {
105
137
final Path agent = Paths .get (jarPath );
106
138
if (!Files .exists (agent )) {
107
139
throw new RuntimeException ("Unable to find java agent at " + jarPath );
108
140
}
141
+ System .out .println ("Agent provided via configuration retrieved" );
142
+ return agent ;
143
+ }
144
+
145
+ System .out .println ("No agent path provided or agent does not exist, checking for cached agent" );
146
+
147
+ final Path agent = Paths .get (project .getProjectDir ().getPath ()).resolve (AGENT_NAME );
148
+ if (Files .exists (agent )) {
149
+ System .out .println ("Agent jar found at " + project .getProjectDir ().getPath ());
109
150
return agent ;
110
151
}
152
+ System .out .println ("Connected to Contrast at: " + connection .getRestApiURL ());
111
153
154
+ System .out .println ("Attempting to retrieve agent from TeamServer" );
112
155
// If no jar is provided, and no jarpath configured, attempt to retrieve the agent from TS
113
156
final byte [] bytes ;
114
157
try {
115
- bytes = connection .getAgent (AgentType .JAVA , config . getOrgUuid () );
158
+ bytes = connection .getAgent (AgentType .JAVA , uuid );
116
159
} catch (IOException e ) {
117
160
throw new RuntimeException ("Failed to retrieve Contrast Java Agent: " + e );
118
161
} catch (UnauthorizedException e ) {
@@ -122,20 +165,23 @@ private Path retrieveAgent(final ContrastSDK connection) {
122
165
}
123
166
124
167
// Save the jar to the 'target' directory
125
- final Path target = Paths .get (getProject () .getProjectDir ().getPath ());
168
+ final Path target = Paths .get (project .getProjectDir ().getPath ());
126
169
try {
127
- FileUtils .forceMkdir (target .toFile ());
170
+ Files .createFile (target );
171
+ } catch (final FileAlreadyExistsException e ) {
172
+ System .out .println ("Project directory already exists" );
128
173
} catch (final IOException e ) {
129
174
throw new RuntimeException ("Unable to create directory " + target , e );
130
175
}
131
176
132
- final Path agent = target .resolve (AGENT_NAME );
177
+ final Path downloadedAgent = target .resolve (AGENT_NAME );
133
178
try {
134
- Files .write (agent , bytes , StandardOpenOption .CREATE , StandardOpenOption .WRITE );
179
+ Files .write (downloadedAgent , bytes , StandardOpenOption .CREATE , StandardOpenOption .WRITE );
135
180
} catch (final IOException e ) {
136
181
throw new RuntimeException ("Unable to save the latest java agent." , e );
137
182
}
138
- return agent ;
183
+ System .out .println ("Agent retrieved from TeamServer" );
184
+ return downloadedAgent ;
139
185
}
140
186
141
187
/** Use ContrastSDK to download agent creds for running the agent */
0 commit comments