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
@@ -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
@@ -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 @@ -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