From 0921e31b21376545335d23b8649473f305bc2e94 Mon Sep 17 00:00:00 2001
From: Olgun Cengiz <olgun.cengiz@vereign.com>
Date: Tue, 6 Nov 2018 16:21:01 +0300
Subject: [PATCH] viam-apis & config changes

---
 Gopkg.toml                  |  2 +-
 config.yaml.sample          | 29 ++++++++++++++++++++++++-----
 handler/generate_keypair.go |  2 +-
 handler/handler.go          |  8 +++++---
 handler/revoke.go           |  2 +-
 main.go                     |  9 +++++----
 server/configs.go           |  7 ++++---
 server/server.go            | 10 ++++++++--
 8 files changed, 49 insertions(+), 20 deletions(-)

diff --git a/Gopkg.toml b/Gopkg.toml
index 8e8e994..1e5c022 100644
--- a/Gopkg.toml
+++ b/Gopkg.toml
@@ -1,5 +1,5 @@
 [[constraint]]
-  branch = "master"
+  branch = "142-Implement_CA_cert"
   name = "code.vereign.com/code/viam-apis"
 
 [[constraint]]
diff --git a/config.yaml.sample b/config.yaml.sample
index 0ec5701..246f7b6 100644
--- a/config.yaml.sample
+++ b/config.yaml.sample
@@ -1,10 +1,29 @@
-dataStorageClientUrl: localhost:7777
-grpcClientUrl: localhost:7877
-restClientUrl: localhost:7878
+# Make sure you have a "config.yaml" file on the root folder of this repo
 
-# Certificate Related Config
+# You can use this sample YAML file to configure your installation
+
+# Connection Information
+dataStorageUrl: localhost:7777
+grpcListenAddress: localhost:7877
+restListenAddress: localhost:7878
+
+# Choose a certificate method for providing PEM strings
+# 1 = Read from file (*.crt and *.key files)
+# 2 = Read from Vault server (this will require additional config information for Vault)
+certificateMethod: 1
+
+# Read Certificates From Folder and Files
 certDir: cert
 certFile: server.crt
 certKey: server.key
 vereignCertFile: vereign_ca.cer
-vereignCertKey: vereign_ca.key
\ No newline at end of file
+vereignCertKey: vereign_ca.key
+caCertFile: ca.crt
+
+# Read Certificates From Vault Server
+vaultAddress: http://10.6.10.119:8200
+vaultToken: 00000000-0000-0000-0000-000000000000
+vaultPath: /developers/data/devteam/cert
+certificateKey: certificateKey
+privateKey: privateKey
+caCertificateKey: caCertificateKey
diff --git a/handler/generate_keypair.go b/handler/generate_keypair.go
index 19f6582..89fa5a0 100644
--- a/handler/generate_keypair.go
+++ b/handler/generate_keypair.go
@@ -39,7 +39,7 @@ func (s *KeyStorageServerImpl) GenerateKeyPair(ctx context.Context,
 	auth := s.CreateAuthentication(ctx)
 
 	client := &client.DataStorageClientImpl{}
-	client.SetUpClient(auth, s.DataStorageUrl, s.CertFilePath)
+	client.SetUpClient(auth, s.DataStorageUrl, s.CertFilePath, s.KeyFilePath, s.CaCertFilePath)
 	defer client.CloseClient()
 
 	generateKeyPairResponse := &api.GenerateKeyPairResponse{}
diff --git a/handler/handler.go b/handler/handler.go
index ab52748..cd29dbd 100644
--- a/handler/handler.go
+++ b/handler/handler.go
@@ -36,6 +36,8 @@ import (
 type KeyStorageServerImpl struct {
 	DataStorageUrl            string
 	CertFilePath              string
+	KeyFilePath 			  string
+	CaCertFilePath			  string
 	VereignCertFilePath       string
 	VereignPrivateKeyFilePath string
 }
@@ -60,7 +62,7 @@ func (s *KeyStorageServerImpl) GetKey(ctx context.Context, in *api.GetKeyRequest
 	auth := s.CreateAuthentication(ctx)
 
 	client := &client.DataStorageClientImpl{}
-	client.SetUpClient(auth, s.DataStorageUrl, s.CertFilePath)
+	client.SetUpClient(auth, s.DataStorageUrl, s.CertFilePath, s.KeyFilePath, s.CaCertFilePath)
 	defer client.CloseClient()
 
 	getKeyResponse := &api.GetKeyResponse{}
@@ -107,7 +109,7 @@ func (s *KeyStorageServerImpl) SetKey(ctx context.Context, in *api.SetKeyRequest
 	auth := s.CreateAuthentication(ctx)
 
 	client := &client.DataStorageClientImpl{}
-	client.SetUpClient(auth, s.DataStorageUrl, s.CertFilePath)
+	client.SetUpClient(auth, s.DataStorageUrl, s.CertFilePath, s.KeyFilePath, s.CaCertFilePath)
 	defer client.CloseClient()
 
 	setKeyResponse := &api.SetKeyResponse{}
@@ -155,7 +157,7 @@ func (s *KeyStorageServerImpl) ReserveKeyUUID(ctx context.Context, in *api.Reser
 	auth := s.CreateAuthentication(ctx)
 
 	client := &client.DataStorageClientImpl{}
-	client.SetUpClient(auth, s.DataStorageUrl, s.CertFilePath)
+	client.SetUpClient(auth, s.DataStorageUrl, s.CertFilePath, s.KeyFilePath, s.CaCertFilePath)
 	defer client.CloseClient()
 
 	reserveKeyUUIDResponse := &api.ReserveKeyUUIDResponse{}
diff --git a/handler/revoke.go b/handler/revoke.go
index 8f757b2..79994ea 100644
--- a/handler/revoke.go
+++ b/handler/revoke.go
@@ -29,7 +29,7 @@ func (s *KeyStorageServerImpl) Revoke(ctx context.Context, in *api.RevokeRequest
 	auth := s.CreateAuthentication(ctx)
 
 	client := &client.DataStorageClientImpl{}
-	client.SetUpClient(auth, s.DataStorageUrl, s.CertFilePath)
+	client.SetUpClient(auth, s.DataStorageUrl, s.CertFilePath, s.KeyFilePath, s.CaCertFilePath)
 	defer client.CloseClient()
 
 	revokeResponse := &api.RevokeResponse{}
diff --git a/main.go b/main.go
index 076a8b7..c6595c4 100644
--- a/main.go
+++ b/main.go
@@ -35,18 +35,19 @@ func main() {
 		return
 	}
 
-	grpcAddress := viper.GetString("grpcClientUrl")
-	restAddress := viper.GetString("restClientUrl")
-	dataStorageAddress := viper.GetString("dataStorageClientUrl")
+	grpcAddress := viper.GetString("grpcListenAddress")
+	restAddress := viper.GetString("restListenAddress")
+	dataStorageAddress := viper.GetString("dataStorageUrl")
 
 	certFilePath := certDir + "/" + viper.GetString("certFile")
 	privateKeyFilePath := certDir + "/" + viper.GetString("certKey")
+	caCertFilePath := certDir + "/" + viper.GetString("caCertFile")
 	vereignCertFilePath := certDir + "/" + viper.GetString("vereignCertFile")
 	vereignPrivateKeyFilePath := certDir + "/" + viper.GetString("vereignCertKey")
 
 	// fire the gRPC server in a goroutine
 	go func() {
-		err := server.StartGRPCServer(grpcAddress, certFilePath, privateKeyFilePath, vereignCertFilePath,
+		err := server.StartGRPCServer(grpcAddress, certFilePath, privateKeyFilePath, caCertFilePath, vereignCertFilePath,
 			vereignPrivateKeyFilePath, dataStorageAddress)
 		if err != nil {
 			log.Fatalf("failed to start gRPC server: %s", err)
diff --git a/server/configs.go b/server/configs.go
index 6354275..6c4cbb9 100644
--- a/server/configs.go
+++ b/server/configs.go
@@ -9,14 +9,15 @@ func SetConfigValues() {
 	// Set Default Values For Config Variables
 
 	// Vereign API Related
-	viper.SetDefault("grpcClientUrl", "localhost:7877")
-	viper.SetDefault("restClientUrl", "localhost:7878")
-	viper.SetDefault("dataStorageClientUrl", "localhost:7777")
+	viper.SetDefault("grpcListenAddress", "localhost:7877")
+	viper.SetDefault("restListenAddress", "localhost:7878")
+	viper.SetDefault("dataStorageUrl", "localhost:7777")
 	
 	// Certificates Related
 	viper.SetDefault("certDir", "cert")
 	viper.SetDefault("certFile", "server.crt")
 	viper.SetDefault("certKey", "server.key")
+	viper.SetDefault("caCertFile", "ca.crt")
 	viper.SetDefault("vereignCertFile", "vereign_ca.cer")
 	viper.SetDefault("vereignCertKey", "vereign_ca.key")
 
diff --git a/server/server.go b/server/server.go
index 8a58bd7..89fb106 100644
--- a/server/server.go
+++ b/server/server.go
@@ -47,6 +47,8 @@ const (
 )
 
 var pkgCertFile string
+var pkgKeyFile string
+var pkgCaCertFile string
 
 func credMatcher(headerName string) (mdName string, ok bool) {
 	if headerName == "Session" {
@@ -70,7 +72,7 @@ func authenticateClient(ctx context.Context, s *handler.KeyStorageServerImpl, in
 		}
 
 		sessionClient := &client.DataStorageClientImpl{}
-		sessionClient.SetUpClient(viamAuth, viper.GetString("dataStorageClientUrl"), pkgCertFile)
+		sessionClient.SetUpClient(viamAuth, viper.GetString("dataStorageUrl"), pkgCertFile, pkgKeyFile, pkgCaCertFile)
 		defer sessionClient.CloseClient()
 
 		if clientAuth.Uuid == viamAuth.Uuid {
@@ -107,8 +109,10 @@ func unaryInterceptor(ctx context.Context, req interface{}, info *grpc.UnaryServ
 	return handler1(ctx, req)
 }
 
-func StartGRPCServer(address, certFilePath, privateKeyFilePath, vereignCertFilePath, vereignPrivateKeyFilePath, dataStorageAddress string) error {
+func StartGRPCServer(address, certFilePath, privateKeyFilePath, caCertFilePath, vereignCertFilePath, vereignPrivateKeyFilePath, dataStorageAddress string) error {
 	pkgCertFile = certFilePath
+	pkgKeyFile = privateKeyFilePath
+	pkgCaCertFile = caCertFilePath
 
 	// create a listener on TCP port
 	lis, err := net.Listen("tcp", address)
@@ -120,6 +124,8 @@ func StartGRPCServer(address, certFilePath, privateKeyFilePath, vereignCertFileP
 	s := handler.KeyStorageServerImpl{
 		DataStorageUrl:            dataStorageAddress,
 		CertFilePath:              certFilePath,
+		KeyFilePath:			   privateKeyFilePath,
+		CaCertFilePath:			   caCertFilePath,
 		VereignCertFilePath:       vereignCertFilePath,
 		VereignPrivateKeyFilePath: vereignPrivateKeyFilePath,
 	}
-- 
GitLab