Skip to content
Snippets Groups Projects
Commit 10619cca authored by Viktor Popov's avatar Viktor Popov
Browse files

Initial commit

parent fe64e75e
No related branches found
No related tags found
1 merge request!140-key-storage-service-api
bin/
vendor/
Gopkg.lock
[[constraint]]
branch = "master"
name = "code.vereign.com/code/viam-apis"
[[constraint]]
branch = "master"
name = "code.vereign.com/code/data-storage-agent"
[[constraint]]
name = "github.com/golang/protobuf"
version = "1.1.0"
[[constraint]]
name = "github.com/grpc-ecosystem/grpc-gateway"
version = "1.4.1"
[[constraint]]
branch = "master"
name = "golang.org/x/net"
[[constraint]]
name = "google.golang.org/grpc"
version = "1.13.0"
[prune]
go-tests = true
unused-packages = true
LICENSE 0 → 100644
Copyright (c) 2018 Vereign AG [https://www.vereign.com]
This is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
\ No newline at end of file
Makefile 0 → 100644
SERVER_OUT := "bin/server"
PKG := "code.vereign.com/code/key-storage-agent"
SERVER_PKG_BUILD := "${PKG}"
PKG_LIST := $(shell go list ${PKG}/... | grep -v /vendor/)
.PHONY: all api server
all: server
dep: ## Get the dependencies
dep ensure -update
server: dep ## Build the binary file for server
@go build -i -v -o $(SERVER_OUT) $(SERVER_PKG_BUILD)
clean: ## Remove previous builds
@rm $(SERVER_OUT)
help: ## Display this help screen
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
-----BEGIN CERTIFICATE-----
MIIDPjCCAiYCCQDpx954xyvbgjANBgkqhkiG9w0BAQsFADBhMQswCQYDVQQGEwJG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iBpgLJBYTvyn7HwluMpjEfOFDosvJZZd/6YWu
vziS1i3jKFEliv3ZNeAw7pTsnW4PAgYzNMSYGH8QPvWXKL6hkJd92LHXRMH+OT6j
0aQsHnjqw+czzzqNYwWr9Kz5AoGBAKvxBeLuUD6x5jAGW9dBsn08MXfYg5WINGox
qngWf+vPmWdOWN81o1BrsbXP67q/AFmaxiD0wnzCnH701w/Am/z074wws/mrcrZQ
c2YMqN39FY+cGWkq5wXZo8Pjr4N/toERM48Un+7qbEmV6OcIHfNgFKZjpIbutqC1
4UnodnPdAoGBANzcLKz99NXBBuAiYHgmOckh6I2RCcORgE6WDbj57qrdMco0NToU
wU9ute0SBEm6VEnBf9i3jlrPZr69f3VT4OFWeo9RoHkVmVDD+LEY5P7xqftj/XZy
2r+uvgkqZWBC3rpAORGq3F7MgD4nFcyssFKwylEGyCVCfyGIsv5+ZKMr
-----END RSA PRIVATE KEY-----
/*
Copyright (c) 2018 Vereign AG [https://www.vereign.com]
This is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package handler
import (
"strings"
"code.vereign.com/code/viam-apis/versions"
"github.com/golang/protobuf/proto"
"code.vereign.com/code/viam-apis/authentication"
"code.vereign.com/code/viam-apis/data-storage-agent/client"
"code.vereign.com/code/viam-apis/key-storage-agent/api"
"code.vereign.com/code/viam-apis/utils"
"golang.org/x/net/context"
"google.golang.org/grpc/metadata"
)
// Server represents the gRPC server
type KeyStorageServerImpl struct {
DataStorageUrl string
CertPath string
}
func (s *KeyStorageServerImpl) CreateAuthentication(ctx context.Context) *authentication.Authentication {
if md, ok := metadata.FromIncomingContext(ctx); ok {
uuid := strings.Join(md["uuid"], "")
session := strings.Join(md["session"], "")
auth := &authentication.Authentication{
Uuid: uuid,
Session: session,
}
return auth
}
return nil
}
func (s *KeyStorageServerImpl) GetPrivateKey(ctx context.Context, in *api.GetPrivateKeyRequest) (*api.GetPrivateKeyResponse, error) {
auth := s.CreateAuthentication(ctx)
client := &client.DataStorageClientImpl{}
client.SetUpClient(auth, s.DataStorageUrl, s.CertPath)
defer client.CloseClient()
data, _ := client.DoGetDataCall("privatekeys", in.Name)
getPrivateKeyResponse := &api.GetPrivateKeyResponse{}
if data.Errors != "" {
getPrivateKeyResponse.Key = nil
getPrivateKeyResponse.StatusList = utils.AddStatus(getPrivateKeyResponse.StatusList, "500", "Internal server error", data.Errors)
} else {
key := &api.PrivateKey{}
proto.Unmarshal(data.Data.Data, key)
getPrivateKeyResponse.Key = key
}
return getPrivateKeyResponse, nil
}
func (s *KeyStorageServerImpl) GetPublicKey(ctx context.Context, in *api.GetPublicKeyRequest) (*api.GetPublicKeyResponse, error) {
auth := s.CreateAuthentication(ctx)
client := &client.DataStorageClientImpl{}
client.SetUpClient(auth, s.DataStorageUrl, s.CertPath)
defer client.CloseClient()
data, _ := client.DoGetDataCall("publickeys", in.Name)
getPublicKeyResponse := &api.GetPublicKeyResponse{}
if data.Errors != "" {
getPublicKeyResponse.Key = nil
getPublicKeyResponse.StatusList = utils.AddStatus(getPublicKeyResponse.StatusList, "500", "Internal server error", data.Errors)
} else {
key := &api.PublicKey{}
proto.Unmarshal(data.Data.Data, key)
getPublicKeyResponse.Key = key
}
return getPublicKeyResponse, nil
}
func (s *KeyStorageServerImpl) SetPrivateKey(ctx context.Context, in *api.SetPrivateKeyRequest) (*api.SetPrivateKeyResponse, error) {
auth := s.CreateAuthentication(ctx)
client := &client.DataStorageClientImpl{}
client.SetUpClient(auth, s.DataStorageUrl, s.CertPath)
defer client.CloseClient()
result, errors, err := client.DoPutDataCall("privatekeys", in.Key.Name, in.Key, versions.EntitiesManagementAgentApiVersion)
setPrivateKeyResponse := &api.SetPrivateKeyResponse{}
if err != nil {
setPrivateKeyResponse.StatusList = utils.AddStatus(setPrivateKeyResponse.StatusList, "500", "error", err.Error())
} else if errors != "" {
setPrivateKeyResponse.StatusList = utils.AddStatus(setPrivateKeyResponse.StatusList, "500", "error", errors)
} else {
setPrivateKeyResponse.StatusList = utils.AddStatus(setPrivateKeyResponse.StatusList, "200", "info", result)
}
return setPrivateKeyResponse, nil
}
func (s *KeyStorageServerImpl) SetPublicKey(ctx context.Context, in *api.SetPublicKeyRequest) (*api.SetPublicKeyResponse, error) {
auth := s.CreateAuthentication(ctx)
client := &client.DataStorageClientImpl{}
client.SetUpClient(auth, s.DataStorageUrl, s.CertPath)
defer client.CloseClient()
result, errors, err := client.DoPutDataCall("publickeys", in.Key.Name, in.Key, versions.EntitiesManagementAgentApiVersion)
setPublicKeyResponse := &api.SetPublicKeyResponse{}
if err != nil {
setPublicKeyResponse.StatusList = utils.AddStatus(setPublicKeyResponse.StatusList, "500", "error", err.Error())
} else if errors != "" {
setPublicKeyResponse.StatusList = utils.AddStatus(setPublicKeyResponse.StatusList, "500", "error", errors)
} else {
setPublicKeyResponse.StatusList = utils.AddStatus(setPublicKeyResponse.StatusList, "200", "info", result)
}
return setPublicKeyResponse, nil
}
#!/bin/bash
PIDFILE="$HOME/tmp/key-storage-agent.pid"
kill -9 `cat $PIDFILE`
main.go 0 → 100644
/*
Copyright (c) 2018 Vereign AG [https://www.vereign.com]
This is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package main
import (
"fmt"
"log"
"code.vereign.com/code/key-storage-agent/server"
"code.vereign.com/code/viam-apis/utils"
)
// main start a gRPC server and waits for connection
func main() {
// TODO this should be done via configuration or even a certificate repository
certDir := utils.GetCertDirFromFlags()
if certDir == "" {
log.Printf("cert-dir cannot be empty")
return
}
grpcAddress := fmt.Sprintf("%s:%d", "localhost", 7877)
restAddress := fmt.Sprintf("%s:%d", "localhost", 7878)
dataStorageAddress := fmt.Sprintf("%s:%d", "localhost", 7777)
certFile := certDir + "/server.crt"
keyFile := certDir + "/server.key"
// fire the gRPC server in a goroutine
go func() {
err := server.StartGRPCServer(grpcAddress, certFile, keyFile, dataStorageAddress)
if err != nil {
log.Fatalf("failed to start gRPC server: %s", err)
}
}()
// fire the REST server in a goroutine
go func() {
err := server.StartRESTServer(restAddress, grpcAddress, certFile)
if err != nil {
log.Fatalf("failed to start gRPC server: %s", err)
}
}()
// infinite loop
log.Printf("Entering infinite loop")
select {}
}
run.sh 0 → 100755
#!/bin/bash
mkdir -p "$HOME/tmp"
PIDFILE="$HOME/tmp/key-storage-agent.pid"
if [ -e "${PIDFILE}" ] && (ps -u $(whoami) -opid= |
grep -P "^\s*$(cat ${PIDFILE})$" &> /dev/null); then
echo "Already running."
exit 99
fi
PATH=$PATH:/usr/local/bin
nohup $GOPATH/src/code.vereign.com/code/key-storage-agent/bin/server --cert-dir $GOPATH/src/code.vereign.com/code/key-storage-agent/cert > $HOME/key-storage-agent.log 2>&1 &
echo $! > "${PIDFILE}"
chmod 644 "${PIDFILE}"
package server
/*
Copyright (c) 2018 Vereign AG [https://www.vereign.com]
This is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import (
"fmt"
"log"
"net"
"net/http"
"strings"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
"golang.org/x/net/context"
"code.vereign.com/code/key-storage-agent/handler"
"code.vereign.com/code/key-storage-agent/session"
"code.vereign.com/code/viam-apis/authentication"
"code.vereign.com/code/viam-apis/data-storage-agent/client"
api "code.vereign.com/code/viam-apis/key-storage-agent/api"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/metadata"
)
// private type for Context keys
type contextKey int
const (
clientIDKey contextKey = iota
)
var pkgCertFile string
func credMatcher(headerName string) (mdName string, ok bool) {
if headerName == "Session" {
return headerName, true
}
return "", false
}
// authenticateAgent check the client credentials
func authenticateClient(ctx context.Context, s *handler.KeyStorageServerImpl, invokedMethod string) (string, error) {
if md, ok := metadata.FromIncomingContext(ctx); ok {
clientAuth := &authentication.Authentication{
Uuid: strings.Join(md["uuid"], ""),
Session: strings.Join(md["session"], ""),
}
viamAuth := &authentication.Authentication{
Uuid: "viam-system",
Session: "viam-session",
}
sessionClient := &client.DataStorageClientImpl{}
sessionClient.SetUpClient(viamAuth, "localhost:7777", pkgCertFile)
defer sessionClient.CloseClient()
if clientAuth.Uuid == viamAuth.Uuid {
if clientAuth.Session != viamAuth.Session {
return "", fmt.Errorf("bad session %s", clientAuth.Session)
}
} else {
if session.CheckSession(clientAuth.Uuid, clientAuth.Session, sessionClient) == false {
return "", fmt.Errorf("bad session %s", clientAuth.Session)
}
}
log.Printf("authenticated uuid: %s", clientAuth.Uuid)
return clientAuth.Uuid, nil
}
return "", fmt.Errorf("missing credentials")
}
// unaryInterceptor call authenticateClient with current context
func unaryInterceptor(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler1 grpc.UnaryHandler) (interface{}, error) {
s, ok := info.Server.(*handler.KeyStorageServerImpl)
fmt.Println("Invoked method: " + info.FullMethod)
if !ok {
return nil, fmt.Errorf("unable to cast server")
}
clientID, err := authenticateClient(ctx, s, info.FullMethod)
if err != nil {
return nil, err
}
ctx = context.WithValue(ctx, clientIDKey, clientID)
return handler1(ctx, req)
}
func StartGRPCServer(address, certFile, keyFile, dataStorageAddress string) error {
pkgCertFile = certFile
// create a listener on TCP port
lis, err := net.Listen("tcp", address)
if err != nil {
return fmt.Errorf("failed to listen: %v", err)
}
// create a server instance
s := handler.KeyStorageServerImpl{
DataStorageUrl: dataStorageAddress,
CertPath: certFile,
}
// Create the TLS credentials
creds, err := credentials.NewServerTLSFromFile(certFile, keyFile)
if err != nil {
return fmt.Errorf("could not load TLS keys: %s", err)
}
// Create an array of gRPC options with the credentials
opts := []grpc.ServerOption{grpc.Creds(creds),
grpc.UnaryInterceptor(unaryInterceptor)}
// create a gRPC server object
grpcServer := grpc.NewServer(opts...)
// attach the CalcMinimumDistance service to the server
api.RegisterKeyStorageServer(grpcServer, &s)
// start the server
log.Printf("starting HTTP/2 gRPC server on %s", address)
if err := grpcServer.Serve(lis); err != nil {
return fmt.Errorf("failed to serve: %s", err)
}
return nil
}
func StartRESTServer(address, grpcAddress, certFile string) error {
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)
defer cancel()
mux := runtime.NewServeMux(runtime.WithIncomingHeaderMatcher(credMatcher))
creds, err := credentials.NewClientTLSFromFile(certFile, "")
if err != nil {
return fmt.Errorf("could not load TLS certificate: %s", err)
}
// Setup the client gRPC options
opts := []grpc.DialOption{grpc.WithTransportCredentials(creds)}
// Register RedisStorageServer
err = api.RegisterKeyStorageHandlerFromEndpoint(ctx, mux, grpcAddress, opts)
if err != nil {
return fmt.Errorf("could not register service RedisStorageServer: %s", err)
}
log.Printf("starting HTTP/1.1 REST server on %s", address)
http.ListenAndServe(address, mux)
return nil
}
/*
Copyright (c) 2018 Vereign AG [https://www.vereign.com]
This is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package server
import (
"log"
"os"
"testing"
"time"
dataStorageServer "code.vereign.com/code/data-storage-agent/server"
"code.vereign.com/code/data-storage-agent/utils"
"code.vereign.com/code/viam-apis/authentication"
ksapi "code.vereign.com/code/viam-apis/key-storage-agent/api"
ksclient "code.vereign.com/code/viam-apis/key-storage-agent/client"
)
const (
dataStorageGrpcAddress = "localhost:7777"
keyStorageGrpcAddress = "localhost:7877"
certFile = "../cert/server.crt"
keyFile = "../cert/server.key"
)
func TestSetAndGetKeys(t *testing.T) {
dataStorageClient := utils.CreateClientFromUuidAndSession("viam-system", "viam-session", dataStorageGrpcAddress, certFile)
keyStorageAuth := &authentication.Authentication{
Uuid: "some-uuid",
Session: "some-session",
}
_, _, _ = dataStorageClient.RenewSession(keyStorageAuth.Uuid, keyStorageAuth.Session)
keyStorageClient := &ksclient.KeyStorageClientImpl{}
keyStorageClient.SetUpClient(keyStorageAuth, keyStorageGrpcAddress, certFile)
defer keyStorageClient.CloseClient()
privateKey := &ksapi.PrivateKey{
Name: "privkey1",
PublicKeyReference: "pubkey1",
Content: []byte{0, 1, 4},
}
statusList, _ := keyStorageClient.DoSetPrivateKey(privateKey)
for _, status := range statusList {
if status.StatusType == "error" {
t.Errorf("DoSetPrivateKey, returned error: %s.", status.Code+":"+status.Description)
}
}
privateKeyResult, statusList, _ := keyStorageClient.DoGetPrivateKey("privkey1")
for _, status := range statusList {
if status.StatusType == "error" {
t.Errorf("DoGetPrivateKey, returned error: %s.", status.Code+":"+status.Description)
}
}
if privateKeyResult.Content == nil || !utils.CompareByteArrays(privateKeyResult.Content, []byte{0, 1, 4}) {
t.Errorf("DoGetPrivateKey, incorrect keyResult.Content, expected: %v, but was: %v",
[]byte{0, 1, 2}, privateKeyResult.Content)
}
publicKey := &ksapi.PublicKey{
Name: "pubkey1",
Content: []byte{3, 4, 2},
Certificate: []byte{5, 1, 6},
}
statusList, _ = keyStorageClient.DoSetPublicKey(publicKey)
for _, status := range statusList {
if status.StatusType == "error" {
t.Errorf("DoSetPublicKey, returned error: %s.", status.Code+":"+status.Description)
}
}
publicKeyResult, statusList, _ := keyStorageClient.DoGetPublicKey("pubkey1")
for _, status := range statusList {
if status.StatusType == "error" {
t.Errorf("DoGetPublicKey, returned error: %s.", status.Code+":"+status.Description)
}
}
if publicKeyResult.Content == nil || !utils.CompareByteArrays(publicKeyResult.Content, []byte{3, 4, 2}) {
t.Errorf("DoGetPublicKey, incorrect publicKeyResult.Content, expected: %v, but was: %v",
[]byte{3, 4, 2}, publicKeyResult.Content)
}
if publicKeyResult.Certificate == nil || !utils.CompareByteArrays(publicKeyResult.Certificate, []byte{5, 1, 6}) {
t.Errorf("DoGetPublicKey, incorrect publicKeyResult.Certificate, expected: %v, but was: %v",
[]byte{5, 1, 6}, publicKeyResult.Certificate)
}
}
func TestMain(m *testing.M) {
// fire the gRPC server in a goroutine
go func() {
err := dataStorageServer.StartGRPCServer(dataStorageGrpcAddress, certFile, keyFile)
if err != nil {
log.Fatalf("failed to start gRPC server: %s", err)
}
}()
// wait a second for the server to start
time.Sleep(time.Duration(1) * time.Second)
// fire the gRPC server in a goroutine
go func() {
err := StartGRPCServer(keyStorageGrpcAddress, certFile, keyFile, dataStorageGrpcAddress)
if err != nil {
log.Fatalf("failed to start gRPC server: %s", err)
}
}()
// wait a second for the server to start
time.Sleep(time.Duration(1) * time.Second)
retCode := m.Run()
os.Exit(retCode)
}
/*
Copyright (c) 2018 Vereign AG [https://www.vereign.com]
This is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package session
import (
client "code.vereign.com/code/viam-apis/data-storage-agent/client"
)
func CheckSession(uuid string, session string, sessionClient *client.DataStorageClientImpl) bool {
hasSession, _, err := sessionClient.HasSession(uuid, session)
if err != nil {
return false
}
return hasSession
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment