forked from grpc/grpc-java
-
Notifications
You must be signed in to change notification settings - Fork 1
/
TlsContextManagerImpl.java
94 lines (81 loc) · 3.65 KB
/
TlsContextManagerImpl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*
* Copyright 2019 The gRPC Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.grpc.xds.internal.sds;
import static com.google.common.base.Preconditions.checkNotNull;
import com.google.common.annotations.VisibleForTesting;
import io.grpc.xds.Bootstrapper;
import io.grpc.xds.EnvoyServerProtoData.DownstreamTlsContext;
import io.grpc.xds.EnvoyServerProtoData.UpstreamTlsContext;
import io.grpc.xds.internal.sds.ReferenceCountingMap.ValueFactory;
/**
* Class to manage {@link SslContextProvider} objects created from inputs we get from xDS. Used by
* gRPC-xds to access the SslContext's and is not public API. This manager manages the life-cycle of
* {@link SslContextProvider} objects as shared resources via ref-counting as described in {@link
* ReferenceCountingMap}.
*/
public final class TlsContextManagerImpl implements TlsContextManager {
private static TlsContextManagerImpl instance;
private final ReferenceCountingMap<UpstreamTlsContext, SslContextProvider> mapForClients;
private final ReferenceCountingMap<DownstreamTlsContext, SslContextProvider> mapForServers;
/** Create a TlsContextManagerImpl instance using the passed in {@link Bootstrapper}. */
@VisibleForTesting public TlsContextManagerImpl(Bootstrapper bootstrapper) {
this(
new ClientSslContextProviderFactory(bootstrapper),
new ServerSslContextProviderFactory(bootstrapper));
}
@VisibleForTesting
TlsContextManagerImpl(
ValueFactory<UpstreamTlsContext, SslContextProvider> clientFactory,
ValueFactory<DownstreamTlsContext, SslContextProvider> serverFactory) {
checkNotNull(clientFactory, "clientFactory");
checkNotNull(serverFactory, "serverFactory");
mapForClients = new ReferenceCountingMap<>(clientFactory);
mapForServers = new ReferenceCountingMap<>(serverFactory);
}
/** Gets the TlsContextManagerImpl singleton. */
public static synchronized TlsContextManagerImpl getInstance() {
if (instance == null) {
instance = new TlsContextManagerImpl(Bootstrapper.getInstance());
}
return instance;
}
@Override
public SslContextProvider findOrCreateServerSslContextProvider(
DownstreamTlsContext downstreamTlsContext) {
checkNotNull(downstreamTlsContext, "downstreamTlsContext");
return mapForServers.get(downstreamTlsContext);
}
@Override
public SslContextProvider findOrCreateClientSslContextProvider(
UpstreamTlsContext upstreamTlsContext) {
checkNotNull(upstreamTlsContext, "upstreamTlsContext");
return mapForClients.get(upstreamTlsContext);
}
@Override
public SslContextProvider releaseClientSslContextProvider(
SslContextProvider clientSslContextProvider) {
checkNotNull(clientSslContextProvider, "clientSslContextProvider");
return mapForClients.release(clientSslContextProvider.getUpstreamTlsContext(),
clientSslContextProvider);
}
@Override
public SslContextProvider releaseServerSslContextProvider(
SslContextProvider serverSslContextProvider) {
checkNotNull(serverSslContextProvider, "serverSslContextProvider");
return mapForServers.release(serverSslContextProvider.getDownstreamTlsContext(),
serverSslContextProvider);
}
}