Skip to content

Commit

Permalink
javafmt
Browse files Browse the repository at this point in the history
  • Loading branch information
radeusgd committed Dec 21, 2023
1 parent ccde4c6 commit 0f5e3d6
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,11 @@
import java.sql.SQLException;
import java.util.List;
import java.util.Properties;

import org.enso.base.net.URISchematic;
import org.enso.base.net.URIWithSecrets;
import org.graalvm.collections.Pair;

/**
* Makes HTTP requests with secrets in either header or query string.
*/
/** Makes HTTP requests with secrets in either header or query string. */
public class EnsoSecretHelper {
/**
* Gets the value of an HideableValue resolving secrets.
Expand All @@ -29,13 +26,12 @@ public class EnsoSecretHelper {
private static String resolveValue(HideableValue value) {
return switch (value) {
case HideableValue.PlainValue plainValue -> plainValue.value();
case HideableValue.SecretValue secretValue -> EnsoSecretReader.readSecret(secretValue.secretId());
case HideableValue.SecretValue secretValue -> EnsoSecretReader.readSecret(
secretValue.secretId());
};
}

/**
* Gets a JDBC connection resolving EnsoKeyValuePair into the properties.
*/
/** Gets a JDBC connection resolving EnsoKeyValuePair into the properties. */
public static Connection getJDBCConnection(String url, Pair<String, HideableValue>[] properties)
throws SQLException {
var javaProperties = new Properties();
Expand All @@ -47,25 +43,41 @@ public static Connection getJDBCConnection(String url, Pair<String, HideableValu
}

/**
* Gets the actual URI with all secrets resolved, so that it can be used to create a request. This value should never be returned to Enso.
* Gets the actual URI with all secrets resolved, so that it can be used to create a request. This
* value should never be returned to Enso.
*/
private static URI resolveURI(URIWithSecrets uri) {
try {
List<Pair<String, String>> resolvedQueryParameters = uri.queryParameters().stream()
.map(p -> Pair.create(p.getLeft(), resolveValue(p.getRight())))
.toList();
Pair<String, String> resolvedUserInfo = uri.userInfo() == null ? null : Pair.create(resolveValue(uri.userInfo().username()), resolveValue(uri.userInfo().password()));
URISchematic resolvedSchematic = new URISchematic(uri.baseUri(), resolvedQueryParameters, resolvedUserInfo);
List<Pair<String, String>> resolvedQueryParameters =
uri.queryParameters().stream()
.map(p -> Pair.create(p.getLeft(), resolveValue(p.getRight())))
.toList();
Pair<String, String> resolvedUserInfo =
uri.userInfo() == null
? null
: Pair.create(
resolveValue(uri.userInfo().username()), resolveValue(uri.userInfo().password()));
URISchematic resolvedSchematic =
new URISchematic(uri.baseUri(), resolvedQueryParameters, resolvedUserInfo);
return resolvedSchematic.build();
} catch (URISyntaxException e) {
// Here we don't display the message of the exception to avoid risking it may leak any secrets.
// Here we don't display the message of the exception to avoid risking it may leak any
// secrets.
// This should never happen in practice.
throw new IllegalStateException("Unexpectedly unable to build a valid URI from the base URI: " + uri + ": " + e.getClass().getCanonicalName());
throw new IllegalStateException(
"Unexpectedly unable to build a valid URI from the base URI: "
+ uri
+ ": "
+ e.getClass().getCanonicalName());
}
}

//** Makes a request with secrets in the query string or headers. **//
public static EnsoHttpResponse makeRequest(HttpClient client, Builder builder, URIWithSecrets uri, List<Pair<String, HideableValue>> headers)
// ** Makes a request with secrets in the query string or headers. **//
public static EnsoHttpResponse makeRequest(
HttpClient client,
Builder builder,
URIWithSecrets uri,
List<Pair<String, HideableValue>> headers)
throws IOException, InterruptedException {

// Build a new URI with the query arguments.
Expand All @@ -85,6 +97,7 @@ public static EnsoHttpResponse makeRequest(HttpClient client, Builder builder, U
var javaResponse = client.send(httpRequest, bodyHandler);

// Extract parts of the response
return new EnsoHttpResponse(renderedURI, javaResponse.headers(), javaResponse.body(), javaResponse.statusCode());
return new EnsoHttpResponse(
renderedURI, javaResponse.headers(), javaResponse.body(), javaResponse.statusCode());
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package org.enso.base.enso_cloud;

/**
* Represents a value that is input of various operation that may contain a Secret.
*/
/** Represents a value that is input of various operation that may contain a Secret. */
public sealed interface HideableValue permits HideableValue.SecretValue, HideableValue.PlainValue {

record SecretValue(String secretId) implements HideableValue {
Expand Down
32 changes: 20 additions & 12 deletions std-bits/base/src/main/java/org/enso/base/net/URISchematic.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
package org.enso.base.net;

import org.graalvm.collections.Pair;

import java.net.IDN;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import org.graalvm.collections.Pair;

/**
* A raw schematic that may be used to build a URI.
* <p>
* It describes how a base URI gets overridden with added query parameters or other options like user-info.
* <p>
* This is the common entry point for building a URI with or without secrets.
*
* <p>It describes how a base URI gets overridden with added query parameters or other options like
* user-info.
*
* <p>This is the common entry point for building a URI with or without secrets.
*/
public record URISchematic(URI baseUri, List<Pair<String, String>> queryParameters, Pair<String, String> userInfo) {
public record URISchematic(
URI baseUri, List<Pair<String, String>> queryParameters, Pair<String, String> userInfo) {
public URI build() throws URISyntaxException {
StringBuilder authorityBuilder = new StringBuilder();
if (userInfo != null) {
Expand All @@ -24,7 +25,11 @@ public URI build() throws URISyntaxException {
throw new IllegalArgumentException("Username within an URI cannot contain ':'.");
}

authorityBuilder.append(URITransformer.encode(username)).append(":").append(URITransformer.encode(password)).append("@");
authorityBuilder
.append(URITransformer.encode(username))
.append(":")
.append(URITransformer.encode(password))
.append("@");
} else if (baseUri.getRawUserInfo() != null) {
authorityBuilder.append(baseUri.getRawUserInfo()).append("@");
}
Expand All @@ -37,7 +42,8 @@ public URI build() throws URISyntaxException {
} else {
boolean hasUserInfo = !authorityBuilder.isEmpty();
if (hasUserInfo) {
throw new IllegalArgumentException("Cannot build an URI with user-info, but without authority.");
throw new IllegalArgumentException(
"Cannot build an URI with user-info, but without authority.");
}
}

Expand All @@ -46,8 +52,7 @@ public URI build() throws URISyntaxException {
authorityBuilder.toString(),
baseUri.getRawPath(),
buildQueryPart(),
baseUri.getRawFragment()
);
baseUri.getRawFragment());
}

private String buildQueryPart() {
Expand All @@ -63,7 +68,10 @@ private String buildQueryPart() {

String name = param.getLeft();
String value = param.getRight();
queryBuilder.append(URITransformer.encodeForQuery(name)).append("=").append(URITransformer.encodeForQuery(value));
queryBuilder
.append(URITransformer.encodeForQuery(name))
.append("=")
.append(URITransformer.encodeForQuery(value));
}

if (queryBuilder.isEmpty()) {
Expand Down
16 changes: 12 additions & 4 deletions std-bits/base/src/main/java/org/enso/base/net/URITransformer.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ public class URITransformer {

/** Removes query parameters from the given URI. */
public static URI removeQueryParameters(URI uri) {
return buildUriFromParts(uri.getScheme(), uri.getRawAuthority(), uri.getRawPath(), null, uri.getRawFragment());
return buildUriFromParts(
uri.getScheme(), uri.getRawAuthority(), uri.getRawPath(), null, uri.getRawFragment());
}

/** Extends the path within a URI with a list of segments. */
Expand All @@ -33,11 +34,17 @@ public static URI extendPath(URI uri, List<String> segments) {
newPath.append(encode(segments.get(i)));
}

return buildUriFromParts(uri.getScheme(), uri.getRawAuthority(), newPath.toString(), uri.getRawQuery(), uri.getRawFragment());
return buildUriFromParts(
uri.getScheme(),
uri.getRawAuthority(),
newPath.toString(),
uri.getRawQuery(),
uri.getRawFragment());
}

/** Builds a URI from raw parts, allowing some of them to be missing. */
public static URI buildUriFromParts(String scheme, String authority, String path, String query, String fragment) {
public static URI buildUriFromParts(
String scheme, String authority, String path, String query, String fragment) {
StringBuilder sb = new StringBuilder();
if (scheme != null) {
sb.append(scheme);
Expand All @@ -51,7 +58,8 @@ public static URI buildUriFromParts(String scheme, String authority, String path
if (path != null && !path.isEmpty()) {
sb.append(path);
} else if (query != null || fragment != null) {
// If we had no path, but we do have a query or a fragment, we need to add a / to precede the ? or #.
// If we had no path, but we do have a query or a fragment, we need to add a / to precede the
// ? or #.
sb.append("/");
}

Expand Down
53 changes: 29 additions & 24 deletions std-bits/base/src/main/java/org/enso/base/net/URIWithSecrets.java
Original file line number Diff line number Diff line change
@@ -1,31 +1,29 @@
package org.enso.base.net;

import org.enso.base.enso_cloud.HideableValue;
import org.graalvm.collections.Pair;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import org.enso.base.enso_cloud.HideableValue;
import org.graalvm.collections.Pair;

/**
* A structure representing a URI that contains parts which may need to be updated once data from secrets is resolved.
* <p>
* The query parameters and user info are stored separately, because they may contain secrets and will only be resolved to plain values within {@link org.enso.base.enso_cloud.EnsoSecretHelper}.
* A structure representing a URI that contains parts which may need to be updated once data from
* secrets is resolved.
*
* <p>The query parameters and user info are stored separately, because they may contain secrets and
* will only be resolved to plain values within {@link org.enso.base.enso_cloud.EnsoSecretHelper}.
*/
public record URIWithSecrets(
URI baseUri,
List<Pair<String, HideableValue>> queryParameters,
UserInfoWithSecrets userInfo
) {
URI baseUri, List<Pair<String, HideableValue>> queryParameters, UserInfoWithSecrets userInfo) {

/**
* Creates a schematic that does not disclose secret values and can be returned to the user.
*/
/** Creates a schematic that does not disclose secret values and can be returned to the user. */
public URISchematic makeSchematicForRender() {
List<Pair<String, String>> renderedParameters = queryParameters.stream()
.map(p -> Pair.create(p.getLeft(), p.getRight().render()))
.toList();
Pair<String, String> renderedUserInfo = userInfo == null ? null : Pair.create(userInfo.username().render(), userInfo.password().render());
List<Pair<String, String>> renderedParameters =
queryParameters.stream().map(p -> Pair.create(p.getLeft(), p.getRight().render())).toList();
Pair<String, String> renderedUserInfo =
userInfo == null
? null
: Pair.create(userInfo.username().render(), userInfo.password().render());
return new URISchematic(baseUri, renderedParameters, renderedUserInfo);
}

Expand All @@ -38,7 +36,8 @@ public URI render() {
}

/**
* Resolves to a proper URI if it does not contain any secrets. If there was a secret, it throws an exception.
* Resolves to a proper URI if it does not contain any secrets. If there was a secret, it throws
* an exception.
*/
public URI safeResolve() {
try {
Expand All @@ -49,10 +48,14 @@ public URI safeResolve() {
}

private URISchematic makeSchematicForSafeResolve() {
List<Pair<String, String>> resolvedParameters = queryParameters.stream()
.map(p -> Pair.create(p.getLeft(), p.getRight().safeResolve()))
.toList();
Pair<String, String> resolvedUserInfo = userInfo == null ? null : Pair.create(userInfo.username().safeResolve(), userInfo.password().safeResolve());
List<Pair<String, String>> resolvedParameters =
queryParameters.stream()
.map(p -> Pair.create(p.getLeft(), p.getRight().safeResolve()))
.toList();
Pair<String, String> resolvedUserInfo =
userInfo == null
? null
: Pair.create(userInfo.username().safeResolve(), userInfo.password().safeResolve());
return new URISchematic(baseUri, resolvedParameters, resolvedUserInfo);
}

Expand All @@ -61,7 +64,8 @@ public String getScheme() {
}

private URI forAuthorityPart() {
// We can ignore secrets in the query part, because they are not used for resolving the authority.
// We can ignore secrets in the query part, because they are not used for resolving the
// authority.
return new URIWithSecrets(baseUri, List.of(), userInfo).safeResolve();
}

Expand Down Expand Up @@ -99,7 +103,8 @@ public String getRawPath() {
}

private URI forQueryPart() {
// We can ignore secrets in the authority part, because they are not used for resolving the query.
// We can ignore secrets in the authority part, because they are not used for resolving the
// query.
return new URIWithSecrets(baseUri, queryParameters, null).safeResolve();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@

import org.enso.base.enso_cloud.HideableValue;

public record UserInfoWithSecrets(HideableValue username, HideableValue password) {
}
public record UserInfoWithSecrets(HideableValue username, HideableValue password) {}
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package org.enso.database;

import org.enso.base.enso_cloud.EnsoSecretHelper;
import org.enso.base.enso_cloud.HideableValue;
import org.graalvm.collections.Pair;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ServiceLoader;
import org.enso.base.enso_cloud.EnsoSecretHelper;
import org.enso.base.enso_cloud.HideableValue;
import org.graalvm.collections.Pair;

/**
* A helper class for accessing the JDBC components.
Expand Down

0 comments on commit 0f5e3d6

Please sign in to comment.