-
Notifications
You must be signed in to change notification settings - Fork 11
/
PopulationRequisitionFulfillerDaemon.kt
267 lines (234 loc) · 9.39 KB
/
PopulationRequisitionFulfillerDaemon.kt
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
// Copyright 2024 The Cross-Media Measurement 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 org.wfanet.measurement.populationdataprovider
import com.google.protobuf.DescriptorProtos
import com.google.protobuf.TypeRegistry
import java.io.File
import java.security.cert.X509Certificate
import java.time.Clock
import java.time.Duration
import kotlinx.coroutines.runBlocking
import org.wfanet.measurement.api.v2alpha.CertificatesGrpcKt.CertificatesCoroutineStub
import org.wfanet.measurement.api.v2alpha.DataProviderCertificateKey
import org.wfanet.measurement.api.v2alpha.ModelReleasesGrpcKt.ModelReleasesCoroutineStub
import org.wfanet.measurement.api.v2alpha.ModelRolloutsGrpcKt.ModelRolloutsCoroutineStub
import org.wfanet.measurement.api.v2alpha.PopulationKey
import org.wfanet.measurement.api.v2alpha.PopulationSpec
import org.wfanet.measurement.api.v2alpha.RequisitionsGrpcKt.RequisitionsCoroutineStub
import org.wfanet.measurement.common.commandLineMain
import org.wfanet.measurement.common.crypto.SigningCerts
import org.wfanet.measurement.common.crypto.SigningKeyHandle
import org.wfanet.measurement.common.crypto.readCertificate
import org.wfanet.measurement.common.crypto.readPrivateKey
import org.wfanet.measurement.common.crypto.tink.loadPrivateKey
import org.wfanet.measurement.common.grpc.TlsFlags
import org.wfanet.measurement.common.grpc.buildMutualTlsChannel
import org.wfanet.measurement.common.grpc.grpcRequireNotNull
import org.wfanet.measurement.common.parseTextProto
import org.wfanet.measurement.common.readByteString
import org.wfanet.measurement.common.throttler.MinimumIntervalThrottler
import org.wfanet.measurement.dataprovider.DataProviderData
import picocli.CommandLine
import picocli.CommandLine.Command
import picocli.CommandLine.Option
class PopulationRequisitionFulfillerFlags {
@CommandLine.Option(
names = ["--kingdom-system-api-target"],
description = ["gRPC target (authority) of the Kingdom system API server"],
required = true,
)
lateinit var target: String
private set
@CommandLine.Option(
names = ["--kingdom-system-api-cert-host"],
description =
[
"Expected hostname (DNS-ID) in the Kingdom system API server's TLS certificate.",
"This overrides derivation of the TLS DNS-ID from --kingdom-system-api-target.",
],
required = false,
)
var certHost: String? = null
private set
/** The PdpSimulator pod's own tls certificates. */
@CommandLine.Mixin
lateinit var tlsFlags: TlsFlags
private set
@CommandLine.Option(
names = ["--data-provider-resource-name"],
description = ["The public API resource name of this data provider."],
required = true,
)
lateinit var dataProviderResourceName: String
private set
@CommandLine.Option(
names = ["--data-provider-certificate-resource-name"],
description = ["The public API resource name for data provider consent signaling."],
required = true,
)
lateinit var dataProviderCertificateResourceName: String
private set
@CommandLine.Option(
names = ["--data-provider-display-name"],
description = ["The display name of this data provider."],
required = true,
)
lateinit var dataProviderDisplayName: String
private set
@CommandLine.Option(
names = ["--data-provider-encryption-private-keyset"],
description = ["The PDP's encryption private Tink Keyset."],
required = true,
)
lateinit var pdpEncryptionPrivateKeyset: File
private set
@CommandLine.Option(
names = ["--data-provider-consent-signaling-private-key-der-file"],
description = ["The PDP's consent signaling private key (DER format) file."],
required = true,
)
lateinit var pdpCsPrivateKeyDerFile: File
private set
@CommandLine.Option(
names = ["--data-provider-consent-signaling-certificate-der-file"],
description = ["The PDP's consent signaling private key (DER format) file."],
required = true,
)
lateinit var pdpCsCertificateDerFile: File
private set
@CommandLine.Option(
names = ["--throttler-minimum-interval"],
description = ["Minimum throttle interval"],
defaultValue = "2s",
)
lateinit var throttlerMinimumInterval: Duration
private set
}
class PopulationRequisitionFulfillerDaemon : Runnable {
@Command(
name = "PopulationRequisitionFulfillerDaemon",
mixinStandardHelpOptions = true,
showDefaultValues = true,
)
@CommandLine.Mixin
protected lateinit var flags: PopulationRequisitionFulfillerFlags
private set
@CommandLine.Option(
names = ["--event-message-descriptor-set"],
description = ["Serialized FileDescriptorSet for the event message and its dependencies."],
required = false,
)
private lateinit var eventMessageDescriptorSetFiles: List<File>
@CommandLine.ArgGroup(exclusive = false, multiplicity = "1..*")
lateinit var populationKeyAndInfo: List<PopulationKeyAndInfo>
private set
class PopulationKeyAndInfo {
@Option(names = ["--population-key"], required = true)
lateinit var populationKey: String
private set
@CommandLine.ArgGroup(exclusive = false, multiplicity = "1")
lateinit var populationInfo: PopulationInfo
private set
}
class PopulationInfo {
@Option(names = ["--population-spec"], required = true)
lateinit var populationSpecFile: File
private set
@Option(names = ["--event-message-descriptor"], required = true)
lateinit var eventMessageDescriptorFile: File
private set
}
override fun run() {
val certificate: X509Certificate =
flags.pdpCsCertificateDerFile.inputStream().use { input -> readCertificate(input) }
val signingKeyHandle =
SigningKeyHandle(
certificate,
readPrivateKey(
flags.pdpCsPrivateKeyDerFile.readByteString(),
certificate.publicKey.algorithm,
),
)
val certificateKey =
DataProviderCertificateKey.fromName(flags.dataProviderCertificateResourceName)!!
val pdpData =
DataProviderData(
flags.dataProviderResourceName,
flags.dataProviderDisplayName,
loadPrivateKey(flags.pdpEncryptionPrivateKeyset),
signingKeyHandle,
certificateKey,
)
val clientCerts =
SigningCerts.fromPemFiles(
certificateFile = flags.tlsFlags.certFile,
privateKeyFile = flags.tlsFlags.privateKeyFile,
trustedCertCollectionFile = flags.tlsFlags.certCollectionFile,
)
val channelTarget = flags.target
val channelCertHost = flags.certHost
val certificatesChannel = buildMutualTlsChannel(channelTarget, clientCerts, channelCertHost)
val certificatesStub = CertificatesCoroutineStub(certificatesChannel)
val requisitionsChannel = buildMutualTlsChannel(channelTarget, clientCerts, channelCertHost)
val requisitionsStub = RequisitionsCoroutineStub(requisitionsChannel)
val modelRolloutsChannel = buildMutualTlsChannel(channelTarget, clientCerts, channelCertHost)
val modelRolloutsStub = ModelRolloutsCoroutineStub(modelRolloutsChannel)
val modelReleasesChannel = buildMutualTlsChannel(channelTarget, clientCerts, channelCertHost)
val modelReleasesStub = ModelReleasesCoroutineStub(modelReleasesChannel)
val throttler = MinimumIntervalThrottler(Clock.systemUTC(), flags.throttlerMinimumInterval)
val typeRegistry = buildTypeRegistry()
val populationInfoMap = buildPopulationInfoMap(populationKeyAndInfo)
var populationRequisitionFulfiller =
PopulationRequisitionFulfiller(
pdpData,
certificatesStub,
requisitionsStub,
throttler,
clientCerts.trustedCertificates,
modelRolloutsStub,
modelReleasesStub,
populationInfoMap,
typeRegistry,
)
runBlocking { populationRequisitionFulfiller.run() }
}
private fun buildTypeRegistry(): TypeRegistry {
val builder = TypeRegistry.newBuilder()
if (::eventMessageDescriptorSetFiles.isInitialized) {
val fileDescriptorSets: List<DescriptorProtos.FileDescriptorSet> =
eventMessageDescriptorSetFiles.map {
parseTextProto(it, DescriptorProtos.FileDescriptorSet.getDefaultInstance())
}
val descriptors = fileDescriptorSets.map { it.descriptorForType }
builder.add(descriptors)
}
return builder.build()
}
private fun buildPopulationInfoMap(
populationKeyAndInfoList: List<PopulationKeyAndInfo>
): Map<PopulationKey, org.wfanet.measurement.populationdataprovider.PopulationInfo> {
return populationKeyAndInfoList.associate {
grpcRequireNotNull(PopulationKey.fromName(it.populationKey)) to
PopulationInfo(
parseTextProto(it.populationInfo.populationSpecFile, PopulationSpec.getDefaultInstance()),
parseTextProto(
it.populationInfo.eventMessageDescriptorFile,
DescriptorProtos.FileDescriptorSet.getDefaultInstance()
)
.descriptorForType,
)
}
}
}
fun main(args: Array<String>) = commandLineMain(PopulationRequisitionFulfillerDaemon(), args)