Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,9 @@ public static class GetConfigLegacy extends OutputHelperMixins.DetailsNoQuery {
public static class UploadFile extends OutputHelperMixins.TableNoQuery {
public static final String CMD_NAME = "upload-file";
}

@Command(aliases = "mfa")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although it's nice to have a short alias, I'm not 100% sure whether I like this alias ('mfa' by itself has a meaning, but doesn't describe what this command does, and also maybe in the future we need to add other MFA-related commands).

Related, can we think of a shorter full command name? I think the option on the login command is now --code, so maybe we can name the command request-code? Any other word for 'request'? Maybe ask Shajaan for suggestions?

Maybe we should reconsider this altogether, thinking first about whether we should improve the MFA handling on the login command. Some potential approaches:

  • Restructure MFA options on login command: --mfa=<code> and --totp=<code>, then have separate mfa command to request MFA code (thus command name matching the login --mfa option name)
  • Integrate MFA request into login command, i.e., --mfa[=<code>], if no code given, we check whether there's a cached code; if not (or cached code results in denied exception due to being expired), we send the 'request MFA' request and then prompt the user to enter the MFA code

In other words, maybe we should take a step back to decide on the most user-friendly approach (while maintaining backward compatibility, possibly marking existing functionality as deprecated).

public static class RequestMfaCode extends OutputHelperMixins.TableNoQuery {
public static final String CMD_NAME = "request-mfa-code";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
subcommands = {
FoDSessionListCommand.class,
FoDSessionLoginCommand.class,
FoDSessionLogoutCommand.class
FoDSessionLogoutCommand.class,
FoDSessionRequestMfaCodeCommand.class
}
)
public class FoDSessionCommands extends AbstractContainerCommand {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,11 @@ public class FoDSessionLoginCommand extends AbstractSessionLoginCommand<FoDSessi
@Mixin private FoDSessionLoginOptions loginOptions;
@Mixin private FoDUnirestInstanceSupplierMixin unirestInstanceSupplierMixin;

private static final String MFA_GUIDANCE = "If MFA is required, provide the security code:\n"
+ " --code <code> (or -c <code>) to provide the security code\n"
+ " --totp to indicate the code is from a TOTP authenticator app";
private static final String MFA_GUIDANCE = "If MFA/TOTP is required, provide the security code:\n"
+ " --code <code> (or -c <code>) for an MFA code\n"
+ " --totp <code> for a TOTP authenticator code\n"
+ " --code <code> --totp (legacy) TOTP code via --code flag\n"
+ "Run 'fcli fod session request-mfa-code' to request an MFA code";

private static final String ERROR_WITH_CODE = "Authentication failed. Possible causes:\n"
+ " - Incorrect username or password\n"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Copyright 2021-2026 Open Text.
*
* The only warranties for products and services of Open Text
* and its affiliates and licensors ("Open Text") are as may
* be set forth in the express warranty statements accompanying
* such products and services. Nothing herein should be construed
* as constituting an additional warranty. Open Text shall not be
* liable for technical or editorial errors or omissions contained
* herein. The information contained herein is subject to change
* without notice.
*/
package com.fortify.cli.fod._common.session.cli.cmd;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fortify.cli.common.log.LogSensitivityLevel;
import com.fortify.cli.common.log.MaskValue;
import com.fortify.cli.common.output.cli.cmd.AbstractOutputCommand;
import com.fortify.cli.common.output.cli.cmd.IJsonNodeSupplier;
import com.fortify.cli.common.output.transform.IActionCommandResultSupplier;
import com.fortify.cli.common.session.cli.mixin.UserCredentialOptions;
import com.fortify.cli.fod._common.output.cli.mixin.FoDOutputHelperMixins;
import com.fortify.cli.fod._common.rest.helper.FoDProductHelper;
import com.fortify.cli.fod._common.session.cli.mixin.FoDSessionLoginOptions;
import com.fortify.cli.fod._common.session.helper.FoDMfaDeliveryType;
import com.fortify.cli.fod._common.session.helper.FoDMfaHelper;

import lombok.Getter;
import picocli.CommandLine.Command;
import picocli.CommandLine.Mixin;
import picocli.CommandLine.Option;

/**
* Command for requesting a Multi-Factor Authentication (MFA) code via Email or SMS.
* @author Sangamesh Vijaykumar
*/
@Command(name = FoDOutputHelperMixins.RequestMfaCode.CMD_NAME, sortOptions = false)
public class FoDSessionRequestMfaCodeCommand extends AbstractOutputCommand implements IJsonNodeSupplier, IActionCommandResultSupplier {
@Getter @Mixin private FoDOutputHelperMixins.RequestMfaCode outputHelper;
@Mixin private FoDSessionLoginOptions.FoDUrlConfigOptions urlConfigOptions;
@Mixin private UserCredentialOptions userCredentials;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The login command already uses an ArgGroup that declares user, password, and tenant options, right? Why not just re-use that here?

@Option(names = {"-t", "--tenant"}, required = true)
@MaskValue(sensitivity = LogSensitivityLevel.low, description = "FOD TENANT")
private String tenant;
@Option(names = {"--delivery-mode", "-m"}, required = true)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's keep this option, but make it optional. If not specified, we try both.

private FoDMfaDeliveryType deliveryMode;

@Override
public JsonNode getJsonNode() {
FoDMfaHelper.requestMfaCode(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use a similar approach as FoDOAuthHelper, i.e., use IFoUserCredentials to pass tenant/user/password as a single method parameter.

urlConfigOptions,
tenant,
userCredentials.getUser(),
userCredentials.getPassword(),
deliveryMode
);

String fodUrl = FoDProductHelper.INSTANCE.getBrowserUrl(urlConfigOptions.getUrl());

ObjectNode result = com.fortify.cli.common.json.JsonHelper.getObjectMapper().createObjectNode();
result.put("fodUrl", fodUrl);
result.put("deliveryMode", deliveryMode.name());
return result;
}

@Override
public boolean isSingular() {
return true;
}

@Override
public String getActionCommandResult() {
return "REQUESTED";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import com.fortify.cli.common.log.MaskValue;
import com.fortify.cli.common.rest.cli.mixin.UrlConfigOptions;
import com.fortify.cli.common.session.cli.mixin.UserCredentialOptions;
import com.fortify.cli.common.util.DisableTest;
import com.fortify.cli.common.util.DisableTest.TestType;
import com.fortify.cli.fod._common.rest.helper.FoDProductHelper;
import com.fortify.cli.fod._common.session.helper.oauth.IFoDClientCredentials;
import com.fortify.cli.fod._common.session.helper.oauth.IFoDUserAuthCode;
Expand Down Expand Up @@ -61,8 +63,9 @@ public static class FoDUserCredentialOptions extends UserCredentialOptions {
@Option(names = {"--code", "-c" }, paramLabel = "<code>", arity = "0..1", interactive = true, echo = false)
@MaskValue(sensitivity = LogSensitivityLevel.low, description = "FOD TOTP/MFA CODE")
@Getter private String securityCode;
@Option(names = {"--totp" })
@Getter private boolean isTotp;
@Option(names = {"--totp"}, arity = "0..1", fallbackValue = "true", paramLabel = "<code>")
@DisableTest(TestType.OPT_ARITY_PRESENT) // arity needed for optional-value flag pattern
@Getter private String totp;
}

public static class FoDClientCredentialOptions implements IFoDClientCredentials {
Expand Down Expand Up @@ -115,7 +118,10 @@ public final boolean hasClientCredentials() {

public boolean hasSecurityCode() {
var userCred = getUserCredentialOptions();
return userCred != null && StringUtils.isNotBlank(userCred.getSecurityCode());
if (userCred == null) { return false; }
var totp = userCred.getTotp();
return (StringUtils.isNotBlank(totp) && !"true".equals(totp))
|| StringUtils.isNotBlank(userCred.getSecurityCode());
}

public String getSecurityCode() {
Expand All @@ -125,15 +131,21 @@ public String getSecurityCode() {

public boolean isTotp() {
var userCred = getUserCredentialOptions();
return userCred != null && userCred.isTotp();
return userCred != null && userCred.getTotp() != null;
}

private String resolveSecurityCode(FoDUserCredentialOptions u) {
var totp = u.getTotp();
return (totp != null && !"true".equals(totp)) ? totp : u.getSecurityCode();
}

public IFoDUserAuthCode getAuthCode() {
var u = getUserCredentialOptions();
if (u == null || StringUtils.isBlank(u.getSecurityCode())) { return null; }
var code = u != null ? resolveSecurityCode(u) : null;
if (StringUtils.isBlank(code)) { return null; }
return BasicFoDUserAuthCode.builder()
.securityCode(u.getSecurityCode())
.isTotp(u.isTotp())
.securityCode(code)
.isTotp(u.getTotp() != null)
.build();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright 2021-2026 Open Text.
*
* The only warranties for products and services of Open Text
* and its affiliates and licensors ("Open Text") are as may
* be set forth in the express warranty statements accompanying
* such products and services. Nothing herein should be construed
* as constituting an additional warranty. Open Text shall not be
* liable for technical or editorial errors or omissions contained
* herein. The information contained herein is subject to change
* without notice.
*/
package com.fortify.cli.fod._common.session.helper;

import com.formkiq.graalvm.annotations.Reflectable;

/**
* Enum representing the delivery types for Multi-Factor Authentication (MFA) codes in Fortify on Demand (FoD).
* @author Sangamesh Vijaykumar
*/
@Reflectable
public enum FoDMfaDeliveryType {
Email("EmailDelivery"),
SMS("SMSDelivery");

private final String apiValue;

FoDMfaDeliveryType(String apiValue) {
this.apiValue = apiValue;
}

public String getApiValue() {
return apiValue;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright 2021-2026 Open Text.
*
* The only warranties for products and services of Open Text
* and its affiliates and licensors ("Open Text") are as may
* be set forth in the express warranty statements accompanying
* such products and services. Nothing herein should be construed
* as constituting an additional warranty. Open Text shall not be
* liable for technical or editorial errors or omissions contained
* herein. The information contained herein is subject to change
* without notice.
*/
package com.fortify.cli.fod._common.session.helper;

import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fortify.cli.common.exception.FcliSimpleException;
import com.fortify.cli.common.http.proxy.helper.ProxyHelper;
import com.fortify.cli.common.json.JsonHelper;
import com.fortify.cli.common.rest.unirest.HttpHeader;
import com.fortify.cli.common.rest.unirest.UnexpectedHttpResponseException;
import com.fortify.cli.common.rest.unirest.UnirestHelper;
import com.fortify.cli.common.rest.unirest.config.IUrlConfig;
import com.fortify.cli.common.rest.unirest.config.UnirestJsonHeaderConfigurer;
import com.fortify.cli.common.rest.unirest.config.UnirestUnexpectedHttpResponseConfigurer;
import com.fortify.cli.common.rest.unirest.config.UnirestUrlConfigConfigurer;

import kong.unirest.UnirestInstance;

/**
* Helper class for requesting Multi-Factor Authentication (MFA) codes in Fortify on Demand (FoD).
* @author Sangamesh Vijaykumar
*/
public class FoDMfaHelper {

public static final void requestMfaCode(IUrlConfig urlConfig, String tenant, String user, char[] password, FoDMfaDeliveryType deliveryType) {
try ( var unirest = UnirestHelper.createUnirestInstance() ) {
configureUnirest(unirest, urlConfig);

ObjectNode requestBody = JsonHelper.getObjectMapper().createObjectNode();
requestBody.put("multiFactorAuthorizationType", deliveryType.getApiValue());
requestBody.put("username", String.format("%s\\%s", tenant, user));
requestBody.put("password", String.valueOf(password));

unirest.post("/api/v3/multi-factor-authorization-code")
.headerReplace(HttpHeader.ACCEPT, "application/json")
.headerReplace(HttpHeader.CONTENT_TYPE, "application/json")
.body(requestBody)
.asEmpty();

//security hardening
java.util.Arrays.fill(password, ' '); // Clear original char array

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Please use proper import
  • This method doesn't own the char[], so clearing it here is a side-effect that callers may not expect (maybe caller wants to do something else with the password). As per best practices, method side effects should be avoided. It would be better to have the caller (that declares/owns the char[]) decide whether the char[] can be cleared or is still needed for something else.
  • Note that we don't explicitly clear password character arrays anywhere else, so not sure how to best make this consistent, i.e., either remove this line, or update all other commands to explicitly clear password character arrays.

} catch ( UnexpectedHttpResponseException e ) {
if ( e.getStatus() == 400 ) {
throw new FcliSimpleException(
"MFA is not enabled for this tenant, or the provided credentials are invalid."
+ " Contact your FoD administrator, then try again."
);
} else if ( e.getStatus() == 401 || e.getStatus() == 403 ) {
throw new FcliSimpleException(
"Authentication failed: invalid username, tenant, or password."
);
}
throw e;
}
}

private static void configureUnirest(UnirestInstance unirest, IUrlConfig urlConfig) {
UnirestUnexpectedHttpResponseConfigurer.configure(unirest);
UnirestUrlConfigConfigurer.configure(unirest, urlConfig);
ProxyHelper.configureProxy(unirest, "fod", urlConfig.getUrl());
UnirestJsonHeaderConfigurer.configure(unirest);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ fcli.fod.session.login.client-secret = FoD client secret.
fcli.fod.session.login.scopes = FoD scopes to request. Default value: ${DEFAULT-VALUE}
fcli.fod.session.login.fod-session = Name for this FoD session. Default value: ${DEFAULT-VALUE}.
fcli.fod.session.login.header = Repeatable option to add custom HTTP headers in requests to FoD for this session, in format `NAME: VALUE`.
fcli.fod.session.login.code = Security code (TOTP from authenticator or MFA code from email/SMS).
fcli.fod.session.login.totp = Indicates the provided code is TOTP from authenticator app (sets do_totp=true).
fcli.fod.session.login.code = MFA security code (from email/SMS). Use 'fcli fod session request-mfa-code' to request a code.
fcli.fod.session.login.totp = TOTP code from an authenticator app. When used as a flag without a value (legacy: --code <code> --totp), the TOTP code must be provided via --code.

fcli.fod.session.logout.usage.header = Terminate FoD session.
fcli.fod.session.logout.usage.description = This command terminates an FoD session previously created \
Expand All @@ -137,6 +137,20 @@ fcli.fod.session.list.usage.description = This command lists all FoD sessions cr
is shown based on locally cached token expiry data. Use '--validate' to verify the actual session \
status against FoD.

fcli.fod.session.request-mfa-code.usage.header = Request Multi-Factor Authentication code via Email or SMS.
fcli.fod.session.request-mfa-code.usage.description = Triggers FoD to send a multi-factor authentication \
code to the specified delivery method for the given tenant and user. Use the received code with \
'fcli fod session login --code <code>' to complete authentication.
fcli.fod.session.request-mfa-code.url = FoD URL, for example https://emea.fortify.com/.
fcli.fod.session.request-mfa-code.tenant = FoD tenant name.
fcli.fod.session.request-mfa-code.user = FoD username.
fcli.fod.session.request-mfa-code.password = FoD password.
fcli.fod.session.request-mfa-code.delivery-mode = Delivery method for the MFA code. Valid values: ${COMPLETION-CANDIDATES}.
fcli.fod.session.request-mfa-code.header = Repeatable option to add custom HTTP headers in requests to FoD, in format `NAME: VALUE`.
fcli.fod.session.request-mfa-code.output.table.args = fodUrl,deliveryMode
fcli.fod.session.request-mfa-code.output.table.header.fodUrl = FoD URL
fcli.fod.session.request-mfa-code.output.table.header.deliveryMode = Delivery Mode

# fcli fod rest
fcli.fod.rest.usage.header = Interact with FoD REST API endpoints.
fcli.fod.rest.usage.description = These commands allow for direct interaction with FoD REST API endpoints, \
Expand Down
Loading