Skip to content

Commit 3288402

Browse files
author
jiangtaoli2016
committed
alts: add client authorization util library
1 parent 074cb73 commit 3288402

2 files changed

Lines changed: 163 additions & 0 deletions

File tree

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright 2019 The gRPC Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.grpc.alts;
18+
19+
import com.google.common.collect.ImmutableList;
20+
import io.grpc.ServerCall;
21+
import io.grpc.Status;
22+
import io.grpc.alts.internal.AltsAuthContext;
23+
import io.grpc.alts.internal.AltsProtocolNegotiator;
24+
25+
/** Utility class for ALTS client authorization. */
26+
public final class AuthorizationUtil {
27+
28+
private AuthorizationUtil() {}
29+
30+
/**
31+
* Given a server call, performs client authorization check, i.e., checks if the client service
32+
* account matches one of the expected service accounts.
33+
*/
34+
public static Status clientAuthorizationCheck(
35+
ServerCall<?, ?> call, ImmutableList<String> expectedServiceAccounts) {
36+
AltsAuthContext altsCtx =
37+
(AltsAuthContext) call.getAttributes().get(AltsProtocolNegotiator.AUTH_CONTEXT_KEY);
38+
if (altsCtx == null) {
39+
return Status.NOT_FOUND.withDescription("Peer ALTS AuthContext not found");
40+
}
41+
for (String serviceAccount : expectedServiceAccounts) {
42+
if (altsCtx.getPeerServiceAccount().equals(serviceAccount)) {
43+
return Status.OK;
44+
}
45+
}
46+
return Status.PERMISSION_DENIED.withDescription(
47+
"Client " + altsCtx.getPeerServiceAccount() + " is not authorized");
48+
}
49+
}
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/*
2+
* Copyright 2019 The gRPC Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.grpc.alts;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
21+
import com.google.common.collect.ImmutableList;
22+
import io.grpc.Attributes;
23+
import io.grpc.Metadata;
24+
import io.grpc.MethodDescriptor;
25+
import io.grpc.ServerCall;
26+
import io.grpc.Status;
27+
import io.grpc.alts.internal.AltsAuthContext;
28+
import io.grpc.alts.internal.AltsProtocolNegotiator;
29+
import io.grpc.alts.internal.HandshakerResult;
30+
import io.grpc.alts.internal.Identity;
31+
import javax.annotation.Nullable;
32+
import org.junit.Test;
33+
import org.junit.runner.RunWith;
34+
import org.junit.runners.JUnit4;
35+
36+
/** Unit tests for {@link AuthorizationUtil}. */
37+
@RunWith(JUnit4.class)
38+
public final class AuthorizationUtilTest {
39+
40+
@Test
41+
public void altsAuthorizationCheck() throws Exception {
42+
Status status =
43+
AuthorizationUtil.clientAuthorizationCheck(
44+
new FakeServerCall(null), ImmutableList.of("Alice"));
45+
assertThat(status.getCode()).isEqualTo(Status.Code.NOT_FOUND);
46+
assertThat(status.getDescription()).startsWith("Peer ALTS AuthContext not found");
47+
status =
48+
AuthorizationUtil.clientAuthorizationCheck(
49+
new FakeServerCall("Alice"), ImmutableList.of("Alice", "Bob"));
50+
assertThat(status.getCode()).isEqualTo(Status.Code.OK);
51+
status =
52+
AuthorizationUtil.clientAuthorizationCheck(
53+
new FakeServerCall("Alice"), ImmutableList.of("Bob", "Joe"));
54+
assertThat(status.getCode()).isEqualTo(Status.Code.PERMISSION_DENIED);
55+
assertThat(status.getDescription()).endsWith("not authorized");
56+
}
57+
58+
private static class FakeServerCall extends ServerCall<String, String> {
59+
final Attributes attrs;
60+
61+
FakeServerCall(@Nullable String peerServiceAccount) {
62+
Attributes.Builder attrsBuilder = Attributes.newBuilder();
63+
if (peerServiceAccount != null) {
64+
HandshakerResult handshakerResult =
65+
HandshakerResult.newBuilder()
66+
.setPeerIdentity(Identity.newBuilder().setServiceAccount(peerServiceAccount))
67+
.build();
68+
AltsAuthContext altsAuthContext = new AltsAuthContext(handshakerResult);
69+
attrsBuilder.set(AltsProtocolNegotiator.AUTH_CONTEXT_KEY, altsAuthContext);
70+
}
71+
attrs = attrsBuilder.build();
72+
}
73+
74+
@Override
75+
public void request(int numMessages) {
76+
throw new AssertionError("Should not be called");
77+
}
78+
79+
@Override
80+
public void sendHeaders(Metadata headers) {
81+
throw new AssertionError("Should not be called");
82+
}
83+
84+
@Override
85+
public void sendMessage(String message) {
86+
throw new AssertionError("Should not be called");
87+
}
88+
89+
@Override
90+
public void close(Status status, Metadata trailers) {
91+
throw new AssertionError("Should not be called");
92+
}
93+
94+
@Override
95+
public boolean isCancelled() {
96+
throw new AssertionError("Should not be called");
97+
}
98+
99+
@Override
100+
public Attributes getAttributes() {
101+
return attrs;
102+
}
103+
104+
@Override
105+
public String getAuthority() {
106+
throw new AssertionError("Should not be called");
107+
}
108+
109+
@Override
110+
public MethodDescriptor<String, String> getMethodDescriptor() {
111+
throw new AssertionError("Should not be called");
112+
}
113+
}
114+
}

0 commit comments

Comments
 (0)