Skip to content

Commit

Permalink
Merge pull request #162 from fortify-ps/general-updates
Browse files Browse the repository at this point in the history
General updates
  • Loading branch information
rsenden authored Nov 29, 2022
2 parents 7f5b610 + b0ff588 commit 82939fe
Show file tree
Hide file tree
Showing 37 changed files with 225 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,36 +24,48 @@
******************************************************************************/
package com.fortify.cli.common.output.cli.cmd.unirest;

import java.util.Arrays;
import java.util.List;

import com.fortify.cli.common.output.cli.mixin.spi.unirest.IUnirestOutputHelper;
import com.fortify.cli.common.output.spi.ISingularSupplier;
import com.fortify.cli.common.rest.cli.cmd.AbstractUnirestRunnerCommand;
import com.fortify.cli.common.session.manager.api.ISessionData;

import io.micronaut.core.annotation.ReflectiveAccess;
import kong.unirest.UnirestInstance;

@ReflectiveAccess
public abstract class AbstractUnirestOutputCommand extends AbstractUnirestRunnerCommand implements ISingularSupplier {
public abstract class AbstractUnirestOutputCommand<D extends ISessionData> extends AbstractUnirestRunnerCommand<D> implements ISingularSupplier {
private static final List<Class<?>> supportedInterfaces = Arrays.asList(
IUnirestBaseRequestSupplier.class,
IUnirestWithSessionDataBaseRequestSupplier.class,
IUnirestJsonNodeSupplier.class,
IUnirestWithSessionDataJsonNodeSupplier.class);
@SuppressWarnings("unchecked")
@Override
protected final Void run(UnirestInstance unirest) {
protected final Void run(UnirestInstance unirest, D sessionData) {
IUnirestOutputHelper outputHelper = getOutputHelper();
if ( isBaseHttpRequestSupplier() ) {
if ( isInstance(IUnirestBaseRequestSupplier.class) ) {
outputHelper.write(unirest, ((IUnirestBaseRequestSupplier)this).getBaseRequest(unirest));
} else if ( isJsonNodeSupplier() ) {
} else if ( isInstance(IUnirestWithSessionDataBaseRequestSupplier.class) ) {
outputHelper.write(unirest, ((IUnirestWithSessionDataBaseRequestSupplier<D>)this).getBaseRequest(unirest, sessionData));
} else if ( isInstance(IUnirestJsonNodeSupplier.class) ) {
outputHelper.write(unirest, ((IUnirestJsonNodeSupplier)this).getJsonNode(unirest));
} else if ( isInstance(IUnirestWithSessionDataJsonNodeSupplier.class) ) {
outputHelper.write(unirest, ((IUnirestWithSessionDataJsonNodeSupplier<D>)this).getJsonNode(unirest, sessionData));
} else {
throw new IllegalStateException(this.getClass().getName()+" must implement exactly one of I[BaseHttpRequest|JsonNodeHolder|JsonNode]Supplier");
throw new IllegalStateException(this.getClass().getName()+" must implement exactly one of "+supportedInterfaces);
}
return null;
}

private boolean isBaseHttpRequestSupplier() {
return (this instanceof IUnirestBaseRequestSupplier)
&& !(this instanceof IUnirestJsonNodeSupplier);
private boolean isInstance(Class<?> clazz) {
return clazz.isAssignableFrom(this.getClass()) &&
supportedInterfaces.stream()
.filter(c->!c.equals(clazz))
.noneMatch(c->c.isAssignableFrom(this.getClass()));
}

private boolean isJsonNodeSupplier() {
return !(this instanceof IUnirestBaseRequestSupplier)
&& (this instanceof IUnirestJsonNodeSupplier);
}
protected abstract IUnirestOutputHelper getOutputHelper();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.fortify.cli.common.output.cli.cmd.unirest;

import com.fortify.cli.common.session.manager.api.ISessionData;

import kong.unirest.HttpRequest;
import kong.unirest.UnirestInstance;

public interface IUnirestWithSessionDataBaseRequestSupplier<D extends ISessionData> {
HttpRequest<?> getBaseRequest(UnirestInstance unirest, D sessionData);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.fortify.cli.common.output.cli.cmd.unirest;

import com.fasterxml.jackson.databind.JsonNode;
import com.fortify.cli.common.session.manager.api.ISessionData;

import kong.unirest.UnirestInstance;

public interface IUnirestWithSessionDataJsonNodeSupplier<D extends ISessionData> {
JsonNode getJsonNode(UnirestInstance unirest, D sessionData);
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import com.fortify.cli.common.output.cli.mixin.writer.StandardOutputWriterFactoryMixin;
import com.fortify.cli.common.output.writer.output.standard.StandardOutputConfig;
import com.fortify.cli.common.session.manager.api.ISessionData;

import io.micronaut.core.annotation.ReflectiveAccess;
import io.micronaut.core.util.StringUtils;
Expand All @@ -37,7 +38,7 @@
import picocli.CommandLine.Parameters;

@ReflectiveAccess
public abstract class AbstractRestCallCommand extends AbstractUnirestRunnerCommand {
public abstract class AbstractRestCallCommand<D extends ISessionData> extends AbstractUnirestRunnerCommand<D> {
public static final String CMD_NAME = "call";
@Mixin private StandardOutputWriterFactoryMixin outputWriterFactory;
@Parameters(index = "0", arity = "1..1", descriptionKey = "api.uri") String uri;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,32 @@
package com.fortify.cli.common.rest.cli.cmd;

import com.fortify.cli.common.cli.cmd.AbstractFortifyCLICommand;
import com.fortify.cli.common.rest.runner.IUnirestRunner;
import com.fortify.cli.common.rest.runner.IUnirestWithSessionDataRunner;
import com.fortify.cli.common.session.manager.api.ISessionData;

import io.micronaut.core.annotation.ReflectiveAccess;
import kong.unirest.UnirestInstance;
import lombok.SneakyThrows;

@ReflectiveAccess
public abstract class AbstractUnirestRunnerCommand extends AbstractFortifyCLICommand implements Runnable {
public abstract class AbstractUnirestRunnerCommand<D extends ISessionData> extends AbstractFortifyCLICommand implements Runnable {
@Override @SneakyThrows
public final void run() {
// TODO Do we want to do anything with the results, like formatting it based on output options?
// Or do we let the actual implementation handle this?
getUnirestRunner().run(this::run);
}

protected abstract IUnirestRunner getUnirestRunner();
protected abstract Void run(UnirestInstance unirest);
protected Void run(UnirestInstance unirest, D sessionData) {
return run(unirest);
}

// TODO Eventually, we'll likely want to change all command implementations to implement
// the run(UnirestInstance, SessionData) method; we can then remove this method and
// make the run(UnirestInstance, Session) method abstract.
protected Void run(UnirestInstance unirest) {
throw new RuntimeException("Command must implement either run(UnirestInstance,SessionData) or run(UnirestInstance)");
}

protected abstract IUnirestWithSessionDataRunner<D> getUnirestRunner();
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,15 @@
import com.fortify.cli.common.rest.cli.mixin.WaitHelperWaitOptions;
import com.fortify.cli.common.rest.wait.WaitHelper;
import com.fortify.cli.common.rest.wait.WaitHelper.WaitHelperBuilder;
import com.fortify.cli.common.session.manager.api.ISessionData;

import io.micronaut.core.annotation.ReflectiveAccess;
import kong.unirest.UnirestInstance;
import lombok.Getter;
import picocli.CommandLine.Mixin;

@ReflectiveAccess
public abstract class AbstractWaitForCommand extends AbstractUnirestRunnerCommand implements IActionCommandResultSupplier, ISingularSupplier {
public abstract class AbstractWaitForCommand<D extends ISessionData> extends AbstractUnirestRunnerCommand<D> implements IActionCommandResultSupplier, ISingularSupplier {
@Getter @Mixin private BasicOutputHelperMixins.WaitFor outputHelper;
@Mixin private WaitHelperControlOptions controlOptions;
@Mixin private WaitHelperWaitOptions waitOptions;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package com.fortify.cli.common.rest.cli.mixin;

import java.util.function.Function;
import java.util.function.BiFunction;

import com.fortify.cli.common.session.manager.api.ISessionData;
import com.fortify.cli.common.util.FixInjection;

import io.micronaut.core.annotation.ReflectiveAccess;
import kong.unirest.UnirestInstance;

@ReflectiveAccess @FixInjection
@ReflectiveAccess
public abstract class AbstractSimpleUnirestRunnerMixin<D extends ISessionData> extends AbstractUnirestRunnerMixin<D> {
@Override
public final <R> R run(Function<UnirestInstance, R> f) {
public final <R> R run(BiFunction<UnirestInstance, D, R> f) {
return run(this::configure, f);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.fortify.cli.common.rest.cli.mixin;

import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.function.BiFunction;

import com.fortify.cli.common.rest.runner.GenericUnirestFactory;
import com.fortify.cli.common.rest.runner.IUnirestRunner;
import com.fortify.cli.common.rest.runner.IUnirestWithSessionDataRunner;
import com.fortify.cli.common.session.cli.mixin.SessionNameMixin;
import com.fortify.cli.common.session.manager.api.ISessionData;
import com.fortify.cli.common.util.FixInjection;
Expand All @@ -15,18 +15,22 @@
import picocli.CommandLine.Mixin;

@ReflectiveAccess @FixInjection
public abstract class AbstractUnirestRunnerMixin<D extends ISessionData> implements IUnirestRunner {
public abstract class AbstractUnirestRunnerMixin<D extends ISessionData> implements IUnirestWithSessionDataRunner<D> {
@Inject private GenericUnirestFactory genericUnirestFactory;
@Mixin private SessionNameMixin.OptionalOption sessionNameMixin;

protected final <R> R run(BiConsumer<UnirestInstance, D> configurer, Function<UnirestInstance, R> f) {
protected final <R> R run(BiConsumer<UnirestInstance, D> configurer, BiFunction<UnirestInstance, D, R> f) {
if ( f == null ) { throw new IllegalStateException("Function may not be null"); }
D sessionData = getSessionData(sessionNameMixin.getSessionName());
D sessionData = getSessionData();
try ( var unirest = genericUnirestFactory.createUnirestInstance() ) {
configurer.accept(unirest, sessionData);
return f.apply(unirest);
return f.apply(unirest, sessionData);
}
}

public D getSessionData() {
return getSessionData(sessionNameMixin.getSessionName());
}

protected abstract D getSessionData(String sessionName);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.fortify.cli.common.rest.runner;

import java.util.function.BiFunction;

import com.fortify.cli.common.session.manager.api.ISessionData;

import kong.unirest.UnirestInstance;

public interface IUnirestWithSessionDataRunner<D extends ISessionData> {
<R> R run(BiFunction<UnirestInstance, D, R> f);
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ while-any=Wait while any records match any of the given states
until-all=Wait until all records match any of the given states
until-any=Wait until any of the records match any of the given states

# StandardWaitHelperProgressMonitorMixin
no-progress=Don't show progress information

# Login and connection options
arggroup.optional.session-name.heading = Session options:%n
session-name[0] = Name for this session
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@

import com.fortify.cli.common.output.cli.cmd.unirest.AbstractUnirestOutputCommand;
import com.fortify.cli.fod.rest.cli.mixin.FoDUnirestRunnerMixin;
import com.fortify.cli.fod.session.manager.FoDSessionData;

import io.micronaut.core.annotation.ReflectiveAccess;
import lombok.Getter;
import picocli.CommandLine.Mixin;

@ReflectiveAccess
public abstract class AbstractFoDOutputCommand extends AbstractUnirestOutputCommand {
public abstract class AbstractFoDOutputCommand extends AbstractUnirestOutputCommand<FoDSessionData> {
@Getter @Mixin FoDUnirestRunnerMixin unirestRunner;
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import com.fortify.cli.common.rest.cli.cmd.AbstractRestCallCommand;
import com.fortify.cli.fod.rest.cli.mixin.FoDUnirestRunnerMixin;
import com.fortify.cli.fod.session.manager.FoDSessionData;

import io.micronaut.core.annotation.ReflectiveAccess;
import lombok.Getter;
Expand All @@ -34,6 +35,6 @@

@ReflectiveAccess
@Command(name = AbstractRestCallCommand.CMD_NAME)
public final class FoDRestCallCommand extends AbstractRestCallCommand {
public final class FoDRestCallCommand extends AbstractRestCallCommand<FoDSessionData> {
@Getter @Mixin private FoDUnirestRunnerMixin unirestRunner;
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import com.fortify.cli.fod.scan.cli.mixin.FoDScanResolverMixin;
import com.fortify.cli.fod.scan.helper.FoDScanHelper;
import com.fortify.cli.fod.scan.helper.FoDScanStatus;
import com.fortify.cli.fod.session.manager.FoDSessionData;

import io.micronaut.core.annotation.ReflectiveAccess;
import lombok.Getter;
Expand All @@ -40,7 +41,7 @@

@ReflectiveAccess
@Command(name = BasicOutputHelperMixins.WaitFor.CMD_NAME)
public class FoDScanWaitForCommand extends AbstractWaitForCommand {
public class FoDScanWaitForCommand extends AbstractWaitForCommand<FoDSessionData> {
@Getter @Mixin FoDUnirestRunnerMixin unirestRunner;
@Mixin private FoDScanResolverMixin.PositionalParameterMulti scansResolver;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

import com.fortify.cli.common.output.cli.cmd.unirest.AbstractUnirestOutputCommand;
import com.fortify.cli.sc_dast.rest.cli.mixin.SCDastUnirestRunnerMixin;
import com.fortify.cli.sc_dast.session.manager.ISCDastSessionData;

import io.micronaut.core.annotation.ReflectiveAccess;
import lombok.Getter;
import picocli.CommandLine.Mixin;

@ReflectiveAccess
public abstract class AbstractSCDastOutputCommand extends AbstractUnirestOutputCommand {
public abstract class AbstractSCDastOutputCommand extends AbstractUnirestOutputCommand<ISCDastSessionData> {
@Getter @Mixin SCDastUnirestRunnerMixin unirestRunner;
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import com.fortify.cli.common.rest.cli.cmd.AbstractRestCallCommand;
import com.fortify.cli.sc_dast.rest.cli.mixin.SCDastUnirestRunnerMixin;
import com.fortify.cli.sc_dast.session.manager.ISCDastSessionData;

import io.micronaut.core.annotation.ReflectiveAccess;
import lombok.Getter;
Expand All @@ -34,6 +35,6 @@

@ReflectiveAccess
@Command(name = AbstractRestCallCommand.CMD_NAME)
public final class SCDastRestCallCommand extends AbstractRestCallCommand {
public final class SCDastRestCallCommand extends AbstractRestCallCommand<ISCDastSessionData> {
@Getter @Mixin private SCDastUnirestRunnerMixin unirestRunner;
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import com.fortify.cli.sc_dast.rest.cli.mixin.SCDastUnirestRunnerMixin;
import com.fortify.cli.sc_dast.scan.cli.mixin.SCDastScanResolverMixin;
import com.fortify.cli.sc_dast.scan.helper.SCDastScanStatus;
import com.fortify.cli.sc_dast.session.manager.ISCDastSessionData;

import io.micronaut.core.annotation.ReflectiveAccess;
import lombok.Getter;
Expand All @@ -38,7 +39,7 @@

@ReflectiveAccess
@Command(name = BasicOutputHelperMixins.WaitFor.CMD_NAME)
public class SCDastScanWaitForCommand extends AbstractWaitForCommand {
public class SCDastScanWaitForCommand extends AbstractWaitForCommand<ISCDastSessionData> {
@Getter @Mixin SCDastUnirestRunnerMixin unirestRunner;
@Mixin private SCDastScanResolverMixin.PositionalParameterMulti scansResolver;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,41 +109,42 @@ fcli.sc-dast.scan-policy.usage.header = Manage ScanCentral DAST scan policies

# fcli sc-dast scan-policy get
fcli.sc-dast.scan-policy.get.usage.header = Get the details for a single scan policy
# TODO options


# fcli sc-dast scan-policy list
fcli.sc-dast.scan-policy.list.usage.header = List scan policies
# TODO options


# fcli sc-dast scan-settings
fcli.sc-dast.scan-settings.usage.header = Manage ScanCentral DAST scan settings

# fcli sc-dast scan-settings get
fcli.sc-dast.scan-settings.get.usage.header = Get the details for a single scan settings definition
# TODO options


# fcli sc-dast scan-settings list
fcli.sc-dast.scan-settings.list.usage.header = List scan settings
# TODO options
fcli.sc-dast.scan-settings.list.modified-after = List settings modified after the given date
fcli.sc-dast.scan-settings.list.modified-before = List settings modified before the given date

# fcli sc-dast sensor
fcli.sc-dast.sensor.usage.header = Manage ScanCentral DAST sensors

# fcli sc-dast sensor disable
fcli.sc-dast.sensor.disable.usage.header = Disable a sensor
# TODO options


# fcli sc-dast sensor enable
fcli.sc-dast.sensor.enable.usage.header = enable a sensor
# TODO options


# fcli sc-dast sensor get
fcli.sc-dast.sensor.get.usage.header = Get the details for a single sensor
# TODO options


# fcli sc-dast sensor list
fcli.sc-dast.sensor.list.usage.header = List sensors
# TODO options



#################################################################################################################
Expand Down
Loading

0 comments on commit 82939fe

Please sign in to comment.