Skip to content

Commit

Permalink
fcli ssc token: Make output more consistent with SSC UI (fixes #194)
Browse files Browse the repository at this point in the history
  • Loading branch information
rsenden committed Dec 14, 2022
1 parent e8eb767 commit 2ed6a89
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ private JsonNode transformRecord(JsonNode recordJsonNode) {
record.put("restToken", token);
record.put("applicationToken", SSCTokenConverter.toApplicationToken(token));
record.remove("token");
SSCTokenHelper.transformTokenRecord(record);
return record;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@

import java.util.HashMap;
import java.util.Map;
import java.util.function.UnaryOperator;

import com.fasterxml.jackson.databind.JsonNode;
import com.fortify.cli.common.output.cli.mixin.BasicOutputHelperMixins;
import com.fortify.cli.common.output.spi.transform.IRecordTransformerSupplier;
import com.fortify.cli.common.rest.runner.config.IUrlConfig;
import com.fortify.cli.common.rest.runner.config.IUserCredentialsConfig;
import com.fortify.cli.common.util.StringUtils;
Expand All @@ -43,7 +45,7 @@

@ReflectiveAccess
@Command(name = BasicOutputHelperMixins.List.CMD_NAME)
public class SSCTokenListCommand extends AbstractSSCTokenCommand {
public class SSCTokenListCommand extends AbstractSSCTokenCommand implements IRecordTransformerSupplier {
@Getter @Mixin private BasicOutputHelperMixins.List outputHelper;
private final SSCQParamGenerator qParamGenerator =
new SSCQParamGenerator()
Expand All @@ -55,6 +57,11 @@ protected JsonNode getJsonNode(SSCTokenHelper tokenHelper, IUrlConfig urlConfig,
return tokenHelper.listTokens(urlConfig, userCredentialsConfig, getQueryParams());
}

@Override
public UnaryOperator<JsonNode> getRecordTransformer() {
return SSCTokenHelper::transformTokenRecord;
}

@Override
public boolean isSingular() {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@
package com.fortify.cli.ssc.token.cli.cmd;

import java.util.function.Predicate;
import java.util.function.UnaryOperator;
import java.util.stream.Stream;

import com.fasterxml.jackson.databind.JsonNode;
import com.fortify.cli.common.output.cli.mixin.BasicOutputHelperMixins;
import com.fortify.cli.common.output.spi.transform.IRecordTransformerSupplier;
import com.fortify.cli.common.rest.runner.config.IUrlConfig;
import com.fortify.cli.common.rest.runner.config.IUserCredentialsConfig;
import com.fortify.cli.ssc.token.helper.SSCTokenHelper;
Expand All @@ -41,7 +43,7 @@

@ReflectiveAccess
@Command(name = BasicOutputHelperMixins.Revoke.CMD_NAME)
public class SSCTokenRevokeCommand extends AbstractSSCTokenCommand {
public class SSCTokenRevokeCommand extends AbstractSSCTokenCommand implements IRecordTransformerSupplier {
@Getter @Mixin private BasicOutputHelperMixins.Revoke outputHelper;
@Parameters(arity="1..") private String[] tokens;

Expand All @@ -57,6 +59,11 @@ protected JsonNode getJsonNode(SSCTokenHelper tokenHelper, IUrlConfig urlConfig,
: tokenHelper.deleteTokensByValue(urlConfig, userCredentialsConfig, tokenValues);
}

@Override
public UnaryOperator<JsonNode> getRecordTransformer() {
return SSCTokenHelper::transformTokenRecord;
}

@Override
public boolean isSingular() {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@
package com.fortify.cli.ssc.token.cli.cmd;

import java.time.OffsetDateTime;
import java.util.function.UnaryOperator;

import com.fasterxml.jackson.databind.JsonNode;
import com.fortify.cli.common.output.cli.mixin.BasicOutputHelperMixins;
import com.fortify.cli.common.output.spi.transform.IRecordTransformerSupplier;
import com.fortify.cli.common.rest.runner.config.IUrlConfig;
import com.fortify.cli.common.rest.runner.config.IUserCredentialsConfig;
import com.fortify.cli.common.util.DateTimePeriodHelper;
Expand All @@ -44,7 +46,7 @@

@ReflectiveAccess
@Command(name = BasicOutputHelperMixins.Update.CMD_NAME)
public class SSCTokenUpdateCommand extends AbstractSSCTokenCommand {
public class SSCTokenUpdateCommand extends AbstractSSCTokenCommand implements IRecordTransformerSupplier {
private static final DateTimePeriodHelper PERIOD_HELPER = DateTimePeriodHelper.byRange(Period.MINUTES, Period.DAYS);
@Getter @Mixin private BasicOutputHelperMixins.Update outputHelper;
@Parameters(arity="1", paramLabel = "token-id", descriptionKey = "fcli.ssc.token.update.token-id") private String tokenId;
Expand All @@ -60,6 +62,11 @@ protected JsonNode getJsonNode(SSCTokenHelper tokenHelper, IUrlConfig urlConfig,
return tokenHelper.updateToken(urlConfig, userCredentialsConfig, tokenId, tokenUpdateRequest);
}

@Override
public UnaryOperator<JsonNode> getRecordTransformer() {
return SSCTokenHelper::transformTokenRecord;
}

@Override
public boolean isSingular() {
return true;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.fortify.cli.ssc.token.helper;

import java.time.Duration;
import java.util.Date;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Stream;
Expand All @@ -25,10 +27,30 @@
import jakarta.inject.Singleton;
import kong.unirest.GetRequest;
import kong.unirest.UnirestInstance;
import picocli.CommandLine.Help.Ansi;

@Singleton @FixInjection
public class SSCTokenHelper {
@Inject private GenericUnirestRunner unirestRunner;
@Inject private GenericUnirestRunner unirestRunner;

public static final JsonNode transformTokenRecord(JsonNode tokenRecord) {
if ( tokenRecord instanceof ObjectNode && tokenRecord.has("terminalDate") ) {
Date terminalDate = JsonHelper.treeToValue(tokenRecord.get("terminalDate"), Date.class);
Duration diff = Duration.between(new Date().toInstant(), terminalDate.toInstant());
String timeRemaining;
if ( diff.toDays()>0 ) {
timeRemaining = String.format("%d days", diff.toDays()+1);
} else if ( diff.toHours()>0 ) {
timeRemaining = String.format("%d hours", diff.toHours()+1);
} else if (diff.toMinutes()>0 ) {
timeRemaining = String.format("%d minutes", diff.toMinutes()+1);
} else {
timeRemaining = Ansi.AUTO.string("0");
}
((ObjectNode)tokenRecord).put("timeRemaining", timeRemaining);
}
return tokenRecord;
}

public final JsonNode listTokens(IUrlConfig urlConfig, IUserCredentialsConfig uc, Map<String,Object> queryParams) {
return unirestRunner.run(unirest->listTokens(unirest, urlConfig, uc, queryParams));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -262,9 +262,9 @@ fcli.ssc.report-template.output.table.options = id,name,type,templateDocId,inUse
# TODO Rename allApplicationRole to universalAccess in command implementation
fcli.ssc.role.output.table.options = id,name,builtIn,allApplicationRole,description
fcli.ssc.role-permission.output.table.options = id,name,dependsOnPermission,description
# TODO Add default table options for revoke/update
fcli.ssc.token.create.output.table.options = id,username,type,restToken,applicationToken,terminalDate,description
fcli.ssc.token.list.output.table.options = id,username,type,creationDate,terminalDate,remainingUsages,description
fcli.ssc.token.output.table.options = id,username,type,creationDate,terminalDate,timeRemaining,description
fcli.ssc.token.create.output.table.options = id,username,type,restToken,applicationToken,terminalDate,timeRemaining,description
fcli.ssc.token.revoke.output.table.options = responseCode
fcli.ssc.token-definition.output.table.options = type,maxDaysToLive,maxUsages,capabilityDescription
fcli.ssc.user.output.table.options = id,entityName,displayName,type,email,isLdap

Expand Down

0 comments on commit 2ed6a89

Please sign in to comment.