Skip to content

Commit

Permalink
Add support for Anchore Engine force and autosubscribe flags when sub…
Browse files Browse the repository at this point in the history
…mitting images for analysis

Signed-off-by: Swathi Gangisetty <swathi@anchore.com>
  • Loading branch information
Swathi Gangisetty committed May 21, 2019
1 parent db10df5 commit 3ab7360
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 6 deletions.
Expand Up @@ -75,6 +75,8 @@ public class AnchoreBuilder extends Builder implements SimpleBuildStep {
private List<AnchoreQuery> inputQueries;
private String policyBundleId = DescriptorImpl.DEFAULT_POLICY_BUNDLE_ID;
private List<Annotation> annotations;
private boolean autoSubscribeTag = DescriptorImpl.DEFAULT_TAG_AUTOSUBSCRIBE;
private boolean force = DescriptorImpl.DEFAULT_USE_FORCE_FLAG;

// Override global config. Supported for anchore-engine mode config only
private String engineurl = DescriptorImpl.EMPTY_STRING;
Expand Down Expand Up @@ -152,6 +154,14 @@ public List<Annotation> getAnnotations() {
return annotations;
}

public boolean getAutoSubscribeTag() {
return autoSubscribeTag;
}

public boolean getForce() {
return force;
}

public String getEngineurl() {
return engineurl;
}
Expand Down Expand Up @@ -245,6 +255,16 @@ public void setAnnotations(List<Annotation> annotations) {
this.annotations = annotations;
}

@DataBoundSetter
public void setAutoSubscribeTag(boolean autoSubscribeTag) {
this.autoSubscribeTag = autoSubscribeTag;
}

@DataBoundSetter
public void setForce(boolean force) {
this.force = force;
}

@DataBoundSetter
public void setEngineurl(String engineurl) {
this.engineurl = engineurl;
Expand Down Expand Up @@ -311,7 +331,7 @@ public void perform(@Nonnull Run<?, ?> run, @Nonnull FilePath workspace, @Nonnul
/* Instantiate config and a new build worker */
config = new BuildConfig(name, policyName, globalWhiteList, anchoreioUser, anchoreioPass, userScripts, engineRetries, bailOnFail,
bailOnWarn, bailOnPluginFail, doCleanup, useCachedBundle, policyEvalMethod, bundleFileOverride, inputQueries, policyBundleId,
annotations, globalConfig.getDebug(), globalConfig.getEnginemode(),
annotations, autoSubscribeTag, force, globalConfig.getDebug(), globalConfig.getEnginemode(),
// messy build time overrides, ugh!
!Strings.isNullOrEmpty(engineurl) ? engineurl : globalConfig.getEngineurl(),
!Strings.isNullOrEmpty(engineuser) ? engineuser : globalConfig.getEngineuser(),
Expand Down Expand Up @@ -419,6 +439,8 @@ public static final class DescriptorImpl extends BuildStepDescriptor<Builder> {
new AnchoreQuery("show-pkg-diffs base"));
public static final String DEFAULT_POLICY_BUNDLE_ID = "";
public static final String EMPTY_STRING = "";
public static final boolean DEFAULT_TAG_AUTOSUBSCRIBE = true;
public static final boolean DEFAULT_USE_FORCE_FLAG = false;

// Global configuration
private boolean debug;
Expand Down
21 changes: 16 additions & 5 deletions src/main/java/com/anchore/jenkins/plugins/anchore/BuildConfig.java
@@ -1,8 +1,8 @@
package com.anchore.jenkins.plugins.anchore;


import java.util.List;
import com.google.common.base.Strings;
import java.util.List;

/**
* Holder for all Anchore configuration - includes global and project level attributes. A convenience class for capturing a snapshot of
Expand All @@ -27,8 +27,9 @@ public class BuildConfig {
private String bundleFileOverride;
private List<AnchoreQuery> inputQueries;
private String policyBundleId;

private List<Annotation> annotations;
private boolean autoSubscribeTag;
private boolean force;

// Global configuration
private boolean debug;
Expand All @@ -46,9 +47,9 @@ public class BuildConfig {
public BuildConfig(String name, String policyName, String globalWhiteList, String anchoreioUser, String anchoreioPass,
String userScripts, String engineRetries, boolean bailOnFail, boolean bailOnWarn, boolean bailOnPluginFail, boolean doCleanup,
boolean useCachedBundle, String policyEvalMethod, String bundleFileOverride, List<AnchoreQuery> inputQueries,
String policyBundleId, List<Annotation> annotations, boolean debug, String enginemode, String engineurl, String engineuser,
String enginepass, boolean engineverify, String containerImageId, String containerId, String localVol, String modulesVol,
boolean useSudo) {
String policyBundleId, List<Annotation> annotations, boolean autoSubscribeTag, boolean force, boolean debug, String enginemode,
String engineurl, String engineuser, String enginepass, boolean engineverify, String containerImageId, String containerId,
String localVol, String modulesVol, boolean useSudo) {
this.name = name;
this.policyName = policyName;
this.globalWhiteList = globalWhiteList;
Expand All @@ -66,6 +67,8 @@ public BuildConfig(String name, String policyName, String globalWhiteList, Strin
this.inputQueries = inputQueries;
this.policyBundleId = policyBundleId;
this.annotations = annotations;
this.autoSubscribeTag = autoSubscribeTag;
this.force = force;
this.debug = debug;
this.enginemode = enginemode;
this.engineurl = engineurl;
Expand Down Expand Up @@ -151,6 +154,14 @@ public List<Annotation> getAnnotations() {
return annotations;
}

public boolean getAutoSubscribeTag() {
return autoSubscribeTag;
}

public boolean getForce() {
return force;
}

public boolean getDebug() {
return debug;
}
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/com/anchore/jenkins/plugins/anchore/BuildWorker.java
Expand Up @@ -3,6 +3,7 @@
import com.anchore.jenkins.plugins.anchore.Util.GATE_ACTION;
import com.anchore.jenkins.plugins.anchore.Util.GATE_SUMMARY_COLUMN;
import com.google.common.base.Strings;
import com.google.common.base.Joiner;
import hudson.AbortException;
import hudson.FilePath;
import hudson.Launcher;
Expand Down Expand Up @@ -220,13 +221,33 @@ private void runAnalyzerEngine() throws AbortException {
for (Map.Entry<String, String> entry : input_image_dfile.entrySet()) {
String tag = entry.getKey();
String dfile = entry.getValue();
List<String> queryList = new ArrayList<>();
String queryStr = null;

console.logInfo("Submitting " + tag + " for analysis");

try (CloseableHttpClient httpclient = makeHttpClient(sslverify)) {
// Prep POST request
String theurl = config.getEngineurl().replaceAll("/+$", "") + "/images";

// Disable autosubscribe if necessary
if (!config.getAutoSubscribeTag()){
queryList.add("autosubscribe=false");
}

// Enable force if necessary
if (config.getForce()) {
queryList.add("force=true");
}

if (!queryList.isEmpty()){
queryStr = Joiner.on('&').skipNulls().join(queryList);
}

if (!Strings.isNullOrEmpty(queryStr)) {
theurl += "?" + queryStr;
}

// Prep request body
JSONObject jsonBody = new JSONObject();
jsonBody.put("tag", tag);
Expand Down
Expand Up @@ -35,6 +35,14 @@
</f:repeatableProperty>
</f:entry>

<f:entry title="Anchore Engine tag auto subscribe" field="autoSubscribeTag">
<f:checkbox name="autoSubscribeTag" checked="${instance.autoSubscribeTag}" default="${descriptor.DEFAULT_TAG_AUTOSUBSCRIBE}"/>
</f:entry>

<f:entry title="Anchore Engine force flag" field="force">
<f:checkbox name="force" checked="${instance.force}" default="${descriptor.DEFAULT_USE_FORCE_FLAG}"/>
</f:entry>

<f:section title="Override Global Configuration">
<f:entry title="Anchore Engine URL" field="engineurl" help="/plugin/anchore-container-scanner/help/help-OverrideAEURL.html">
<f:textbox name="engineurl" default=""/>
Expand Down
@@ -0,0 +1,6 @@
<div>

If selected or set to 'true', the Anchore Container Image Scanner step will instruct Anchore Engine to automatically begin watching
the added tag for updates from registry. Default value: 'true'

</div>
@@ -0,0 +1,6 @@
<div>

If selected or set to 'true', the Anchore Container Image Scanner step will send API requests to Anchore Engine with force flag enabled
(force=true query parameter). Default value: 'false'

</div>

0 comments on commit 3ab7360

Please sign in to comment.