Skip to content

Commit a3f5fe9

Browse files
authored
concord-server: do not create UserPrincipal for API keys without userId (#1218)
UserPrincipal#user should not be null. Instead, we should skip `UserPrincipal` entirely when authenticating via an API key without the user.
1 parent d54d1e2 commit a3f5fe9

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

server/impl/src/main/java/com/walmartlabs/concord/server/security/UserPrincipal.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
import java.io.Serializable;
2727
import java.util.UUID;
2828

29+
import static java.util.Objects.requireNonNull;
30+
2931
/**
3032
* <b>Note:</b> this class is serialized when user principals are stored in
3133
* the process state. It must maintain backward compatibility.
@@ -46,8 +48,8 @@ public static UserPrincipal assertCurrent() {
4648
private final UserEntry user;
4749

4850
public UserPrincipal(String realm, UserEntry user) {
49-
this.realm = realm;
50-
this.user = user;
51+
this.realm = requireNonNull(realm);
52+
this.user = requireNonNull(user);
5153
}
5254

5355
public String getRealm() {

server/impl/src/main/java/com/walmartlabs/concord/server/security/apikey/ApiKeyRealm.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@
3737
import org.apache.shiro.subject.PrincipalCollection;
3838

3939
import javax.inject.Inject;
40-
import java.util.Arrays;
40+
import java.util.ArrayList;
41+
import java.util.List;
4142

4243
public class ApiKeyRealm extends AuthorizingRealm {
4344

@@ -80,17 +81,21 @@ protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token)
8081
.field("apiKeyId", t.getKeyId())
8182
.log();
8283

83-
UserPrincipal p = new UserPrincipal(REALM_NAME, u);
84-
return new SimpleAccount(Arrays.asList(p, t), t.getKey(), getName());
84+
List<Object> principals = new ArrayList<>();
85+
if (u != null) {
86+
principals.add(new UserPrincipal(REALM_NAME, u));
87+
}
88+
principals.add(t);
89+
90+
return new SimpleAccount(principals, t.getKey(), getName());
8591
}
8692

8793
@Override
8894
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
89-
UserPrincipal p = principals.oneByType(UserPrincipal.class);
90-
if (!REALM_NAME.equals(p.getRealm())) {
95+
ApiKey principal = principals.oneByType(ApiKey.class);
96+
if (principal == null) {
9197
return null;
9298
}
93-
9499
return SecurityUtils.toAuthorizationInfo(principals);
95100
}
96101
}

0 commit comments

Comments
 (0)