1# Copyright 2020 Google LLC 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# http://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS-IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14"""Tink Primitive Testing Service in Python.""" 15 16from concurrent import futures 17import sys 18 19from absl import app 20from absl import flags 21import grpc 22from tink import aead 23from tink import daead 24from tink import hybrid 25from tink import jwt 26from tink import mac 27from tink import prf 28from tink import signature 29from tink import streaming_aead 30from tink.integration import gcpkms 31 32from tink.testing import fake_kms 33from protos import testing_api_pb2_grpc 34import jwt_service 35import services 36 37from tink.integration import awskms 38 39FLAGS = flags.FLAGS 40 41flags.DEFINE_integer('port', 10000, 'The port of the server.') 42GCP_CREDENTIALS_PATH = flags.DEFINE_string( 43 'gcp_credentials_path', '', 'Google Cloud KMS credentials path.') 44GCP_KEY_URI = flags.DEFINE_string( 45 'gcp_key_uri', '', 'Google Cloud KMS key URL of the form: ' 46 'gcp-kms://projects/*/locations/*/keyRings/*/cryptoKeys/*.') 47AWS_CREDENTIALS_PATH = flags.DEFINE_string('aws_credentials_path', '', 48 'AWS KMS credentials path.') 49AWS_KEY_URI = flags.DEFINE_string( 50 'aws_key_uri', '', 'AWS KMS key URL of the form: ' 51 'aws-kms://arn:aws:kms:<region>:<account-id>:key/<key-id>.') 52 53 54def init_tink() -> None: 55 """Initializes Tink registering the required primitives.""" 56 57 aead.register() 58 daead.register() 59 hybrid.register() 60 mac.register() 61 prf.register() 62 signature.register() 63 streaming_aead.register() 64 jwt.register_jwt_mac() 65 jwt.register_jwt_signature() 66 fake_kms.register_client() 67 awskms.AwsKmsClient.register_client( 68 key_uri=AWS_KEY_URI.value, credentials_path=AWS_CREDENTIALS_PATH.value) 69 70 gcpkms.GcpKmsClient.register_client( 71 key_uri=GCP_KEY_URI.value, credentials_path=GCP_CREDENTIALS_PATH.value 72 ) 73 74 75def main(unused_argv): 76 init_tink() 77 78 server = grpc.server(futures.ThreadPoolExecutor(max_workers=2)) 79 testing_api_pb2_grpc.add_MetadataServicer_to_server( 80 services.MetadataServicer(), server) 81 testing_api_pb2_grpc.add_KeysetServicer_to_server( 82 services.KeysetServicer(), server) 83 testing_api_pb2_grpc.add_AeadServicer_to_server( 84 services.AeadServicer(), server) 85 testing_api_pb2_grpc.add_DeterministicAeadServicer_to_server( 86 services.DeterministicAeadServicer(), server) 87 testing_api_pb2_grpc.add_MacServicer_to_server( 88 services.MacServicer(), server) 89 testing_api_pb2_grpc.add_PrfSetServicer_to_server(services.PrfSetServicer(), 90 server) 91 testing_api_pb2_grpc.add_HybridServicer_to_server( 92 services.HybridServicer(), server) 93 testing_api_pb2_grpc.add_SignatureServicer_to_server( 94 services.SignatureServicer(), server) 95 testing_api_pb2_grpc.add_StreamingAeadServicer_to_server( 96 services.StreamingAeadServicer(), server) 97 testing_api_pb2_grpc.add_JwtServicer_to_server(jwt_service.JwtServicer(), 98 server) 99 used_port = server.add_secure_port('[::]:%d' % FLAGS.port, 100 grpc.local_server_credentials()) 101 server.start() 102 print('Server started on port ' + str(used_port)) 103 print(' (stderr) Server started on port ' + str(used_port), file=sys.stderr) 104 server.wait_for_termination() 105 106 107if __name__ == '__main__': 108 app.run(main) 109