Skip to content

Commit

Permalink
feat: Configurable connect & socket timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
rsenden committed Aug 18, 2023
1 parent 2cf4d96 commit 3015bb5
Show file tree
Hide file tree
Showing 10 changed files with 133 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*******************************************************************************
* (c) Copyright 2021 Micro Focus or one of its affiliates
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including without
* limitation the rights to use, copy, modify, merge, publish, distribute,
* sublicense, and/or sell copies of the Software, and to permit persons to
* whom the Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
* KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
* PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
******************************************************************************/
package com.fortify.cli.common.rest.cli.mixin;

import com.fortify.cli.common.rest.runner.config.IConnectionConfig;
import com.fortify.cli.common.util.DateTimePeriodHelper;
import com.fortify.cli.common.util.DateTimePeriodHelper.Period;

import io.micronaut.core.annotation.ReflectiveAccess;
import kong.unirest.Config;
import lombok.Getter;
import picocli.CommandLine.Option;

/**
* Configure connection options to a remote system
* </pre>
* @author Ruud Senden
*/
@ReflectiveAccess
public abstract class ConnectionConfigOptions implements IConnectionConfig {
private static final DateTimePeriodHelper periodHelper = DateTimePeriodHelper.byRange(Period.SECONDS, Period.MINUTES);

@Option(names = {"--insecure", "-k"}, required = false, description = "Disable SSL checks", defaultValue = "false", order=6)
@Getter private Boolean insecureModeEnabled;

@Option(names = {"--socket-timeout"}, required = false, description = "Socket timeout for this session, for example 30s (30 seconds), 5m (5 minutes)", order=7)
private String socketTimeout;

@Option(names = {"--connect-timeout"}, required = false, description = "Connection timeout for this session in seconds, for example 30s (30 seconds), 5m (5 minutes)", order=8)
private String connectTimeout;

@Override
public int getConnectTimeoutInMillis() {
return connectTimeout==null ? getDefaultConnectTimeoutInMillis() : (int)periodHelper.parsePeriodToMillis(connectTimeout);
}

@Override
public int getSocketTimeoutInMillis() {
return socketTimeout==null ? getDefaultSocketTimeoutInMillis() : (int)periodHelper.parsePeriodToMillis(socketTimeout);
}

protected int getDefaultSocketTimeoutInMillis() {
return Config.DEFAULT_SOCKET_TIMEOUT;
}

protected int getDefaultConnectTimeoutInMillis() {
return Config.DEFAULT_CONNECT_TIMEOUT;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,10 @@
* @author Ruud Senden
*/
@ReflectiveAccess
public class UrlConfigOptions implements IUrlConfig {
public class UrlConfigOptions extends ConnectionConfigOptions implements IUrlConfig {
@Option(names = {"--url"}, required = true, order=1)
@Getter private String url;

@Option(names = {"--insecure", "-k"}, required = false, description = "Disable SSL checks", defaultValue = "false", order=6)
@Getter private Boolean insecureModeEnabled;

public boolean hasUrlConfig() {
return url!=null;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*******************************************************************************
* (c) Copyright 2020 Micro Focus or one of its affiliates
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including without
* limitation the rights to use, copy, modify, merge, publish, distribute,
* sublicense, and/or sell copies of the Software, and to permit persons to
* whom the Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
* KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
* PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
* IN THE SOFTWARE.
******************************************************************************/
package com.fortify.cli.common.rest.runner.config;

public interface IConnectionConfig {
int getConnectTimeoutInMillis();
int getSocketTimeoutInMillis();
Boolean getInsecureModeEnabled();
default boolean isInsecureModeEnabled() {
return Boolean.TRUE.equals(getInsecureModeEnabled());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,6 @@
******************************************************************************/
package com.fortify.cli.common.rest.runner.config;

public interface IUrlConfig {
public interface IUrlConfig extends IConnectionConfig {
String getUrl();
Boolean getInsecureModeEnabled();
default boolean isInsecureModeEnabled() {
return Boolean.TRUE.equals(getInsecureModeEnabled());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ public static final void configure(UnirestInstance unirestInstance, IUrlConfig u
if ( urlConfig==null ) { throw new IllegalArgumentException("URL configuration may not be null"); }
unirestInstance.config()
.defaultBaseUrl(normalizeUrl(urlConfig.getUrl()))
.verifySsl(!urlConfig.isInsecureModeEnabled());
.verifySsl(!urlConfig.isInsecureModeEnabled())
.socketTimeout(urlConfig.getSocketTimeoutInMillis())
.connectTimeout(urlConfig.getConnectTimeoutInMillis());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
@Data @ReflectiveAccess @NoArgsConstructor @AllArgsConstructor @Builder
public class UrlConfig implements IUrlConfig {
private String url;
private int socketTimeoutInMillis;
private int connectTimeoutInMillis;
private Boolean insecureModeEnabled;

public static final UrlConfig from(IUrlConfig other) {
Expand All @@ -48,7 +50,9 @@ public static final UrlConfigBuilder builderFrom(IUrlConfig other) {
if ( other!=null ) {
builder = builder
.url(other.getUrl())
.insecureModeEnabled(other.isInsecureModeEnabled());
.insecureModeEnabled(other.isInsecureModeEnabled())
.connectTimeoutInMillis(other.getConnectTimeoutInMillis())
.socketTimeoutInMillis(other.getSocketTimeoutInMillis());
}
return builder;
}
Expand All @@ -58,6 +62,8 @@ public static final UrlConfigBuilder builderFrom(IUrlConfig other, IUrlConfig ov
if ( other!=null && overrides!=null ) {
override(overrides.getUrl(), builder::url);
override(overrides.getInsecureModeEnabled(), builder::insecureModeEnabled);
builder.connectTimeoutInMillis(overrides.getConnectTimeoutInMillis())
.socketTimeoutInMillis(overrides.getSocketTimeoutInMillis());
}
return builder;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,24 @@

public class FoDSessionLoginOptions {
@ArgGroup(exclusive = false, multiplicity = "1", headingKey = "arggroup.fod-connection-options.heading", order = 1)
@Getter private UrlConfigOptions urlConfigOptions = new UrlConfigOptions();
@Getter private UrlConfigOptions urlConfigOptions = new FoDUrlConfigOptions();

@ArgGroup(exclusive = false, multiplicity = "1", headingKey = "arggroup.fod-authentication-options.heading", order = 2)
@Getter private FoDAuthOptions authOptions = new FoDAuthOptions();



public String[] getScopes() {
return new String[]{"api-tenant"}; // TODO make scopes configurable
}

public static class FoDUrlConfigOptions extends UrlConfigOptions {
@Override
protected int getDefaultSocketTimeoutInMillis() {
return 600000;
}
}

public static class FoDAuthOptions {
@ArgGroup(exclusive = true, multiplicity = "1", order = 3)
@Getter private FoDCredentialOptions credentialOptions = new FoDCredentialOptions();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,18 @@
******************************************************************************/
package com.fortify.cli.sc_dast.session.cli.mixin;

import com.fortify.cli.common.rest.cli.mixin.ConnectionConfigOptions;
import com.fortify.cli.common.rest.runner.config.IUrlConfig;

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

@ReflectiveAccess
public class SCDastUrlConfigOptions implements IUrlConfig {
public class SCDastUrlConfigOptions extends ConnectionConfigOptions implements IUrlConfig {
@Option(names = {"--ssc-url"}, required = true, order=1)
@Getter private String url;

@Option(names = {"--insecure", "-k"}, required = false, description = "Disable SSL checks", defaultValue = "false", order=6)
@Getter private Boolean insecureModeEnabled;

public boolean hasUrlConfig() {
return url!=null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,18 @@
******************************************************************************/
package com.fortify.cli.sc_sast.session.cli.mixin;

import com.fortify.cli.common.rest.cli.mixin.ConnectionConfigOptions;
import com.fortify.cli.common.rest.runner.config.IUrlConfig;

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

@ReflectiveAccess
public class SCSastUrlConfigOptions implements IUrlConfig {
public class SCSastUrlConfigOptions extends ConnectionConfigOptions implements IUrlConfig {
@Option(names = {"--ssc-url"}, required = true, order=1)
@Getter private String url;

@Option(names = {"--insecure", "-k"}, required = false, description = "Disable SSL checks", defaultValue = "false", order=6)
@Getter private Boolean insecureModeEnabled;

public boolean hasUrlConfig() {
return url!=null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,19 @@
******************************************************************************/
package com.fortify.cli.ssc.appversion_artifact.cli.cmd.imprt.debricked;

import com.fortify.cli.common.rest.cli.mixin.ConnectionConfigOptions;
import com.fortify.cli.common.rest.runner.config.IUrlConfig;

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

@ReflectiveAccess
public class DebrickedUrlConfigOptions implements IUrlConfig {
public class DebrickedUrlConfigOptions extends ConnectionConfigOptions implements IUrlConfig {
// For now, this option is hidden as there is only the single debricked.com SaaS instance
@Option(names = {"--debricked-url"}, required = true, order=1, defaultValue = "https://debricked.com", hidden = true)
@Getter private String url;

@Option(names = {"--insecure", "-k"}, required = false, description = "Disable SSL checks", defaultValue = "false", order=6)
@Getter private Boolean insecureModeEnabled;

public boolean hasUrlConfig() {
return url!=null;
}
Expand Down

0 comments on commit 3015bb5

Please sign in to comment.