This repository has been archived by the owner on Jun 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
752 additions
and
187 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"flag" | ||
"log" | ||
"time" | ||
|
||
re "github.com/neicnordic/sda-download/internal/reencrypt" | ||
"google.golang.org/grpc" | ||
"google.golang.org/grpc/credentials/insecure" | ||
) | ||
|
||
var ( | ||
addr = flag.String("addr", "localhost:5051", "the address to connect to") | ||
publickey = flag.String("publickey", "NZfoJzFcOli3UWi/7U624h6fv2PufL1i2QPK8JkpmFg=", "Name to greet") | ||
fileid = flag.String("fileid", "urn:neic:001-002", "Name to greet") | ||
) | ||
|
||
func main() { | ||
flag.Parse() | ||
// Set up a connection to the server. | ||
conn, err := grpc.Dial(*addr, grpc.WithTransportCredentials(insecure.NewCredentials())) | ||
if err != nil { | ||
log.Fatalf("did not connect: %v", err) | ||
} | ||
defer conn.Close() | ||
c := re.NewReencryptClient(conn) | ||
|
||
// Contact the server and print out its response. | ||
ctx, cancel := context.WithTimeout(context.Background(), time.Second) | ||
defer cancel() | ||
r, err := c.ReencryptHeader(ctx, &re.ReencryptRequest{Fileid: *fileid, Publickey: *publickey}) | ||
if err != nil { | ||
log.Fatalf("could not greet: %v", err) | ||
} | ||
log.Printf("Greeting: %s", string(r.GetHeader())) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net" | ||
"strings" | ||
|
||
log "github.com/sirupsen/logrus" | ||
|
||
"github.com/neicnordic/crypt4gh/keys" | ||
"github.com/neicnordic/crypt4gh/model/headers" | ||
"golang.org/x/crypto/chacha20poly1305" | ||
|
||
"github.com/neicnordic/sda-download/internal/config" | ||
"github.com/neicnordic/sda-download/internal/database" | ||
re "github.com/neicnordic/sda-download/internal/reencrypt" | ||
"google.golang.org/grpc" | ||
) | ||
|
||
// server is used to implement reencrypt.ReEncryptServer. | ||
type server struct { | ||
re.UnimplementedReencryptServer | ||
} | ||
|
||
// init is run before main, it sets up configuration and other required things | ||
func init() { | ||
log.Info("(1/5) Loading configuration") | ||
|
||
// Load configuration | ||
conf, err := config.NewConfig("reencrypt") | ||
if err != nil { | ||
log.Panicf("configuration loading failed, reason: %v", err) | ||
} | ||
config.Config = *conf | ||
|
||
// Connect to database | ||
db, err := database.NewDB(conf.DB) | ||
if err != nil { | ||
log.Panicf("database connection failed, reason: %v", err) | ||
} | ||
defer db.Close() | ||
database.DB = db | ||
|
||
} | ||
|
||
// Reencrypt implements reencrypt.ReEncryptServer | ||
func (s *server) ReencryptHeader(ctx context.Context, in *re.ReencryptRequest) (*re.ReencryptResponse, error) { | ||
log.Debugf("Received Public key: %v", in.GetPublickey()) | ||
log.Debugf("Received fileid: %v", in.GetFileid()) | ||
// Get file header | ||
fileDetails, err := database.GetFile(in.GetFileid()) | ||
if err != nil { | ||
|
||
return nil, err | ||
} | ||
|
||
newReaderPublicKey, err := keys.ReadPublicKey(strings.NewReader("-----BEGIN CRYPT4GH PUBLIC KEY-----\n" + in.GetPublickey() + "\n-----END CRYPT4GH PUBLIC KEY-----\n")) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
newReaderPublicKeyList := [][chacha20poly1305.KeySize]byte{} | ||
newReaderPublicKeyList = append(newReaderPublicKeyList, newReaderPublicKey) | ||
|
||
log.Debugf("header: %v", fileDetails.Header) | ||
log.Debugf("crypt4ghkey path: %v", *config.Config.Grpc.Crypt4GHKey) | ||
|
||
newheader, err := headers.ReEncryptHeader(fileDetails.Header, *config.Config.Grpc.Crypt4GHKey, newReaderPublicKeyList) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &re.ReencryptResponse{Header: newheader}, nil | ||
} | ||
|
||
func main() { | ||
lis, err := net.Listen("tcp", fmt.Sprintf(":%d", *&config.Config.Grpc.Port)) | ||
if err != nil { | ||
log.Fatalf("failed to listen: %v", err) | ||
} | ||
s := grpc.NewServer() | ||
re.RegisterReencryptServer(s, &server{}) | ||
log.Printf("server listening at %v", lis.Addr()) | ||
if err := s.Serve(lis); err != nil { | ||
log.Fatalf("failed to serve: %v", err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.