xref: /aosp_15_r20/external/tink/testing/go/testing_server.go (revision e7b1675dde1b92d52ec075b0a92829627f2c52a5)
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//
15///////////////////////////////////////////////////////////////////////////////
16
17// Package main is implements an gRPC server for testing_api.
18package main
19
20import (
21	"context"
22	"fmt"
23	"log"
24	"net"
25
26	"flag"
27	// context is used to cancel outstanding requests
28	"google.golang.org/api/option"
29	"google.golang.org/grpc"
30	"github.com/google/tink/go/core/registry"
31	"github.com/google/tink/go/integration/awskms"
32	"github.com/google/tink/go/integration/gcpkms"
33	"github.com/google/tink/go/testing/fakekms"
34	"github.com/google/tink/testing/go/services"
35	pbgrpc "github.com/google/tink/testing/go/protos/testing_api_go_grpc"
36)
37
38var (
39	port            = flag.Int("port", 10000, "The server port")
40	gcpCredFilePath = flag.String("gcp_credentials_path", "", "Google Cloud KMS credentials path")
41	gcpKeyURI       = flag.String("gcp_key_uri", "", "Google Cloud KMS key URL of the form: gcp-kms://projects/*/locations/*/keyRings/*/cryptoKeys/*.")
42	awsCredFilePath = flag.String("aws_credentials_path", "", "AWS KMS credentials path")
43	awsKeyURI       = flag.String("aws_key_uri", "", "AWS KMS key URL of the form: aws-kms://arn:aws:kms:<region>:<account-id>:key/<key-id>.")
44)
45
46func main() {
47	flag.Parse()
48	client, err := fakekms.NewClient("fake-kms://")
49	if err != nil {
50		log.Fatalf("fakekms.NewClient failed: %v", err)
51	}
52	registry.RegisterKMSClient(client)
53
54	gcpClient, err := gcpkms.NewClientWithOptions(context.Background(), *gcpKeyURI, option.WithCredentialsFile(*gcpCredFilePath))
55	if err != nil {
56		log.Fatalf("gcpkms.NewClientWithOptions failed: %v", err)
57	}
58	registry.RegisterKMSClient(gcpClient)
59
60	awsClient, err := awskms.NewClientWithOptions(*awsKeyURI, awskms.WithCredentialPath(*awsCredFilePath))
61	if err != nil {
62		log.Fatalf("awskms.NewClientWithOptions failed: %v", err)
63	}
64	registry.RegisterKMSClient(awsClient)
65
66	lis, err := net.Listen("tcp", fmt.Sprintf(":%d", *port))
67	if err != nil {
68		log.Fatalf("Server failed to listen: %v", err)
69	}
70	log.Printf("Server is now listening on port: %d", *port)
71	server := grpc.NewServer()
72	if err != nil {
73		log.Fatalf("Failed to create new grpcprod server: %v", err)
74	}
75	pbgrpc.RegisterMetadataServer(server, &services.MetadataService{})
76	pbgrpc.RegisterKeysetServer(server, &services.KeysetService{})
77	pbgrpc.RegisterAeadServer(server, &services.AEADService{})
78	pbgrpc.RegisterDeterministicAeadServer(server, &services.DeterministicAEADService{})
79	pbgrpc.RegisterHybridServer(server, &services.HybridService{})
80	pbgrpc.RegisterJwtServer(server, &services.JWTService{})
81	pbgrpc.RegisterMacServer(server, &services.MacService{})
82	pbgrpc.RegisterPrfSetServer(server, &services.PrfSetService{})
83	pbgrpc.RegisterSignatureServer(server, &services.SignatureService{})
84	pbgrpc.RegisterStreamingAeadServer(server, &services.StreamingAEADService{})
85	server.Serve(lis)
86}
87