Skip to content

Commit

Permalink
Fixes spotless errors
Browse files Browse the repository at this point in the history
Signed-off-by: Darshit Chanpura <dchanp@amazon.com>
  • Loading branch information
DarshitChanpura committed Sep 27, 2023
1 parent 204c9aa commit d8c7ca2
Show file tree
Hide file tree
Showing 17 changed files with 266 additions and 267 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,12 @@
import org.opensearch.transport.TransportService;
import org.opensearch.watcher.ResourceWatcherService;

public final class OpenSearchSecurityPlugin extends OpenSearchSecuritySSLPlugin implements ClusterPlugin, MapperPlugin, IdentityPlugin, ExtensionAwarePlugin {
public final class OpenSearchSecurityPlugin extends OpenSearchSecuritySSLPlugin
implements
ClusterPlugin,
MapperPlugin,
IdentityPlugin,
ExtensionAwarePlugin {

private static final String KEYWORD = ".keyword";
private static final Logger actionTrace = LogManager.getLogger("opendistro_security_action_trace");
Expand Down Expand Up @@ -234,18 +239,16 @@ public final class OpenSearchSecurityPlugin extends OpenSearchSecuritySSLPlugin
private volatile OpensearchDynamicSetting<Boolean> transportPassiveAuthSetting;

public static Setting RESERVED_INDICES_SETTING = Setting.listSetting(
"reserved_indices",
List.of(),
Function.identity(),
Property.ExtensionScope
"reserved_indices",
List.of(),
Function.identity(),
Property.ExtensionScope
);

public static Setting PERMISSIONS_SETTING = Setting.groupSetting("permissions.", Property.ExtensionScope);

public static Setting SEND_BACKEND_ROLES_SETTING = Setting.boolSetting("send_backend_roles", false, Property.ExtensionScope);



public static boolean isActionTraceEnabled() {
return actionTrace.isTraceEnabled();
}
Expand Down Expand Up @@ -1947,8 +1950,6 @@ public TokenManager getTokenManager() {
return tokenManager;
}



public static class GuiceHolder implements LifecycleComponent {

private static RepositoriesService repositoriesService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

import com.google.common.collect.ImmutableList;
import org.greenrobot.eventbus.Subscribe;
Expand Down Expand Up @@ -153,8 +152,8 @@ public void accept(RestChannel channel) throws Exception {
user.getName(),
service,
tokenDuration,
new HashSet<>(mappedRoles),
new HashSet<>(user.getRoles()),
new HashSet<>(mappedRoles),
new HashSet<>(user.getRoles()),
roleSecurityMode
);
builder.field("authenticationToken", token);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ public User authenticate(final AuthCredentials credentials) {
return user;
}


@Override
public boolean exists(User user) {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,6 @@ private CipherMode(final int opmode) {
}
}

//TODO: RYAN PUT YOUR LOGIC HERE TO CONNECT WITH https://raw.githubusercontent.com/cwperks/security/bf22d4a4e7e716f818e4ce48767590a9f0e53f38/src/main/java/org/opensearch/security/authtoken/jwt/EncryptionDecryptionUtil.java
// TODO: RYAN PUT YOUR LOGIC HERE TO CONNECT WITH
// https://raw.githubusercontent.com/cwperks/security/bf22d4a4e7e716f818e4ce48767590a9f0e53f38/src/main/java/org/opensearch/security/authtoken/jwt/EncryptionDecryptionUtil.java
}
33 changes: 14 additions & 19 deletions src/main/java/org/opensearch/security/authtoken/jwt/JwtVendor.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
package org.opensearch.security.authtoken.jwt;

import java.time.Instant;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.function.LongSupplier;
Expand All @@ -32,10 +30,7 @@
import org.apache.logging.log4j.Logger;

import org.opensearch.common.settings.Settings;
import org.opensearch.extensions.ExtensionsSettings;
import org.opensearch.security.OpenSearchSecurityPlugin;
import org.opensearch.security.ssl.util.ExceptionUtils;
import static org.opensearch.security.OpenSearchSecurityPlugin.SEND_BACKEND_ROLES_SETTING;

public class JwtVendor {
private static final Logger logger = LogManager.getLogger(JwtVendor.class);
Expand Down Expand Up @@ -95,7 +90,7 @@ static JsonWebKey createJwkFromSettings(Settings settings) throws Exception {

if (jwkSettings.isEmpty()) {
throw new Exception(
"Settings for signing key is missing. Please specify at least the option signing_key with a shared secret."
"Settings for signing key is missing. Please specify at least the option signing_key with a shared secret."
);
}

Expand All @@ -110,13 +105,13 @@ static JsonWebKey createJwkFromSettings(Settings settings) throws Exception {
}

public String createJwt(
String issuer,
String subject,
String audience,
Integer expirySeconds,
Set<String> roles,
Set<String> backendRoles,
boolean roleSecurityMode
String issuer,
String subject,
String audience,
Integer expirySeconds,
Set<String> roles,
Set<String> backendRoles,
boolean roleSecurityMode
) throws Exception {
final long nowAsMillis = timeProvider.getAsLong();
final Instant nowAsInstant = Instant.ofEpochMilli(timeProvider.getAsLong());
Expand Down Expand Up @@ -162,12 +157,12 @@ public String createJwt(

if (logger.isDebugEnabled()) {
logger.debug(
"Created JWT: "
+ encodedJwt
+ "\n"
+ jsonMapReaderWriter.toJson(jwt.getJwsHeaders())
+ "\n"
+ JwtUtils.claimsToJson(jwt.getClaims())
"Created JWT: "
+ encodedJwt
+ "\n"
+ jsonMapReaderWriter.toJson(jwt.getJwsHeaders())
+ "\n"
+ JwtUtils.claimsToJson(jwt.getClaims())
);
}

Expand Down
48 changes: 24 additions & 24 deletions src/main/java/org/opensearch/security/identity/SecuritySubject.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,28 @@

public class SecuritySubject implements Subject {

private ThreadContext threadContext;

public SecuritySubject() {}

public void setThreadContext(ThreadContext threadContext) {
this.threadContext = threadContext;
}

@Override
public Principal getPrincipal() {
if (threadContext == null) {
return NamedPrincipal.UNAUTHENTICATED;
}
final User user = (User) threadContext.getTransient(ConfigConstants.OPENDISTRO_SECURITY_USER);
if (user == null) {
return NamedPrincipal.UNAUTHENTICATED;
}
return new NamedPrincipal(user.getName());
}

@Override
public void authenticate(AuthToken authToken) {
// TODO implement this - replace with logic from SecurityRestFilter
}
private ThreadContext threadContext;

public SecuritySubject() {}

public void setThreadContext(ThreadContext threadContext) {
this.threadContext = threadContext;
}

@Override
public Principal getPrincipal() {
if (threadContext == null) {
return NamedPrincipal.UNAUTHENTICATED;
}
final User user = (User) threadContext.getTransient(ConfigConstants.OPENDISTRO_SECURITY_USER);
if (user == null) {
return NamedPrincipal.UNAUTHENTICATED;
}
return new NamedPrincipal(user.getName());
}

@Override
public void authenticate(AuthToken authToken) {
// TODO implement this - replace with logic from SecurityRestFilter
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,83 +26,83 @@

public class SecurityTokenManager implements TokenManager {

public static Settings DEMO_SETTINGS = Settings.builder()
.put(
"signing_key",
Base64.getEncoder()
.encodeToString(
"This is my super secret that no one in the universe will ever be able to guess in a bajillion years".getBytes(
StandardCharsets.UTF_8
)
)
)
.put("encryption_key", Base64.getEncoder().encodeToString("encryptionKey".getBytes(StandardCharsets.UTF_8)))
.build();
public static Settings DEMO_SETTINGS = Settings.builder()
.put(
"signing_key",
Base64.getEncoder()
.encodeToString(
"This is my super secret that no one in the universe will ever be able to guess in a bajillion years".getBytes(
StandardCharsets.UTF_8
)
)
)
.put("encryption_key", Base64.getEncoder().encodeToString("encryptionKey".getBytes(StandardCharsets.UTF_8)))
.build();

private ConfigModel configModel;
private ClusterService cs;
private ThreadPool threadPool;
private UserService userService;
private ConfigModel configModel;
private ClusterService cs;
private ThreadPool threadPool;
private UserService userService;

public SecurityTokenManager(ClusterService cs, ThreadPool threadPool, UserService userService) {
this.cs = cs;
this.threadPool = threadPool;
this.userService = userService;
}
public SecurityTokenManager(ClusterService cs, ThreadPool threadPool, UserService userService) {
this.cs = cs;
this.threadPool = threadPool;
this.userService = userService;
}

private JwtVendor jwtVendor = new JwtVendor(DEMO_SETTINGS, Optional.empty());
private JwtVendor jwtVendor = new JwtVendor(DEMO_SETTINGS, Optional.empty());

@Override
public AuthToken issueOnBehalfOfToken(Subject subject, OnBehalfOfClaims claims) {
User user = threadPool.getThreadContext().getTransient(ConfigConstants.OPENDISTRO_SECURITY_USER);
if (user == null) {
throw new OpenSearchSecurityException("Cannot issue on behalf of token.");
}
if (Strings.isNullOrEmpty(claims.getAudience())) {
throw new OpenSearchSecurityException("Cannot issue on behalf of token without an audience claim.");
}
@Override
public AuthToken issueOnBehalfOfToken(Subject subject, OnBehalfOfClaims claims) {
User user = threadPool.getThreadContext().getTransient(ConfigConstants.OPENDISTRO_SECURITY_USER);
if (user == null) {
throw new OpenSearchSecurityException("Cannot issue on behalf of token.");
}
if (Strings.isNullOrEmpty(claims.getAudience())) {
throw new OpenSearchSecurityException("Cannot issue on behalf of token without an audience claim.");
}

final TransportAddress caller = threadPool.getThreadContext().getTransient(ConfigConstants.OPENDISTRO_SECURITY_REMOTE_ADDRESS);
final TransportAddress caller = threadPool.getThreadContext().getTransient(ConfigConstants.OPENDISTRO_SECURITY_REMOTE_ADDRESS);

Set<String> mappedRoles = mapRoles(user, caller);
String encodedJwt = null;
Set<String> mappedRoles = mapRoles(user, caller);
String encodedJwt = null;

try {
encodedJwt = jwtVendor.createJwt(
cs.getClusterName().value(),
user.getName(),
claims.getAudience(),
300,
mappedRoles,
user.getRoles(),
false
);
} catch (Exception e) {
throw new RuntimeException(e);
}
return new BearerAuthToken(encodedJwt);
}
try {
encodedJwt = jwtVendor.createJwt(
cs.getClusterName().value(),
user.getName(),
claims.getAudience(),
300,
mappedRoles,
user.getRoles(),
false
);
} catch (Exception e) {
throw new RuntimeException(e);
}
return new BearerAuthToken(encodedJwt);
}

@Override
public AuthToken issueServiceAccountToken(String extensionUniqueId) throws OpenSearchSecurityException {
try {
return new BasicAuthToken(this.userService.generateAuthToken(extensionUniqueId));
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Override
public AuthToken issueServiceAccountToken(String extensionUniqueId) throws OpenSearchSecurityException {
try {
return new BasicAuthToken(this.userService.generateAuthToken(extensionUniqueId));
} catch (Exception e) {
throw new RuntimeException(e);
}
}

@Override
public Subject authenticateToken(AuthToken authToken) {
return null;
}
@Override
public Subject authenticateToken(AuthToken authToken) {
return null;
}

public Set<String> mapRoles(final User user, final TransportAddress caller) {
return this.configModel.mapSecurityRoles(user, caller);
}
public Set<String> mapRoles(final User user, final TransportAddress caller) {
return this.configModel.mapSecurityRoles(user, caller);
}

@Subscribe
public void onConfigModelChanged(ConfigModel configModel) {
this.configModel = configModel;
}
@Subscribe
public void onConfigModelChanged(ConfigModel configModel) {
this.configModel = configModel;
}
}
Loading

0 comments on commit d8c7ca2

Please sign in to comment.