Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

concord-server: simplify UserInfo #1061

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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 @@ -20,6 +20,7 @@
* =====
*/

import com.fasterxml.jackson.databind.ObjectMapper;
import com.walmartlabs.concord.sdk.Constants;
import com.walmartlabs.concord.server.process.pipelines.processors.signing.Signing;
import com.walmartlabs.concord.server.user.UserManager;
Expand All @@ -31,7 +32,7 @@
public class CurrentUserInfoProcessor extends UserInfoProcessor {

@Inject
public CurrentUserInfoProcessor(UserManager userManager, Signing signing) {
super(Constants.Request.CURRENT_USER_KEY, userManager, signing);
public CurrentUserInfoProcessor(UserManager userManager, Signing signing, ObjectMapper objectMapper) {
super(Constants.Request.CURRENT_USER_KEY, userManager, signing, objectMapper);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
* =====
*/

import com.fasterxml.jackson.databind.ObjectMapper;
import com.walmartlabs.concord.sdk.Constants;
import com.walmartlabs.concord.server.process.pipelines.processors.signing.Signing;
import com.walmartlabs.concord.server.user.UserManager;
Expand All @@ -31,7 +32,7 @@
public class InitiatorUserInfoProcessor extends UserInfoProcessor {

@Inject
public InitiatorUserInfoProcessor(UserManager userManager, Signing signing) {
super(Constants.Request.INITIATOR_KEY, userManager, signing);
public InitiatorUserInfoProcessor(UserManager userManager, Signing signing, ObjectMapper objectMapper) {
super(Constants.Request.INITIATOR_KEY, userManager, signing, objectMapper);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,20 @@
* =====
*/

import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.node.TextNode;
import com.walmartlabs.concord.server.process.Payload;
import com.walmartlabs.concord.server.process.pipelines.processors.signing.Signing;
import com.walmartlabs.concord.server.sdk.ConcordApplicationException;
import com.walmartlabs.concord.server.user.UserInfoProvider.BaseUserInfo;
import com.walmartlabs.concord.server.user.UserInfoProvider;
import com.walmartlabs.concord.server.user.UserManager;
import org.immutables.value.Value;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

import static java.util.Objects.requireNonNull;

/**
* Collects and stores the current user's data.
Expand All @@ -40,51 +43,43 @@ public abstract class UserInfoProcessor implements PayloadProcessor {
private final String key;
private final UserManager userManager;
private final Signing signing;
private final ObjectMapper objectMapper;

public UserInfoProcessor(String key,
UserManager userManager,
Signing signing,
ObjectMapper objectMapper) {

public UserInfoProcessor(String key, UserManager userManager, Signing signing) {
this.key = key;
this.userManager = userManager;
this.signing = signing;
this.key = requireNonNull(key);
this.userManager = requireNonNull(userManager);
this.signing = requireNonNull(signing);
this.objectMapper = requireNonNull(objectMapper);
}

@Override
public Payload process(Chain chain, Payload payload) {
BaseUserInfo info = userManager.getCurrentUserInfo();
var info = userManager.getCurrentUserInfo();

var result = objectMapper.convertValue(info, ObjectNode.class);
if (signing.isEnabled()) {
info = sign(info);
Optional.ofNullable(info.username())
.map(this::sign)
.ifPresent(signature -> result.set("usernameSignature", signature));
}

Map<String, BaseUserInfo> m = new HashMap<>();
Map<String, UserInfoProvider.UserInfo> m = new HashMap<>();
m.put(key, info);

payload = payload.mergeValues(Payload.CONFIGURATION, m);

return chain.process(payload);
}

private BaseUserInfo sign(BaseUserInfo i) {
if (i == null || i.username() == null) {
return i;
}

private TextNode sign(String username) {
try {
String s = signing.sign(i.username());
return SignedUserInfo.from(i).usernameSignature(s).build();
return TextNode.valueOf(signing.sign(username));
} catch (Exception e) {
throw new ConcordApplicationException("Error while singing process data: " + e.getMessage(), e);
}
}

@Value.Immutable
@JsonSerialize(as = ImmutableSignedUserInfo.class)
@JsonDeserialize(as = ImmutableSignedUserInfo.class)
public interface SignedUserInfo extends BaseUserInfo {

String usernameSignature();

public static ImmutableSignedUserInfo.Builder from(BaseUserInfo i) {
return ImmutableSignedUserInfo.builder().from(i);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
* =====
*/

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import org.immutables.value.Value;
Expand All @@ -44,9 +43,15 @@ public interface UserInfoProvider {
UserInfo getInfo(UUID id, String username, String userDomain);

UUID create(String username, String domain, String displayName, String email, Set<String> roles);

@JsonInclude(JsonInclude.Include.NON_EMPTY)
interface BaseUserInfo {

@Value.Immutable
@JsonSerialize(as = ImmutableUserInfo.class)
@JsonDeserialize(as = ImmutableUserInfo.class)
interface UserInfo {

static ImmutableUserInfo.Builder builder() {
return ImmutableUserInfo.builder();
}

@Nullable
UUID id();
Expand All @@ -69,14 +74,4 @@ interface BaseUserInfo {
@Nullable
Map<String, Object> attributes();
}

@Value.Immutable
@JsonSerialize(as = ImmutableUserInfo.class)
@JsonDeserialize(as = ImmutableUserInfo.class)
interface UserInfo extends BaseUserInfo {

static ImmutableUserInfo.Builder builder() {
return ImmutableUserInfo.builder();
}
}
}
Loading