Skip to content

Commit

Permalink
chore: remove encryption key from KeyPair in proto
Browse files Browse the repository at this point in the history
  • Loading branch information
revoltez committed Aug 14, 2024
1 parent 4b6f4d3 commit 288f792
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 69 deletions.
12 changes: 2 additions & 10 deletions cmd/trustedpods/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"path/filepath"

"github.com/comrade-coop/apocryph/pkg/abi"
tpcrypto "github.com/comrade-coop/apocryph/pkg/crypto"
"github.com/comrade-coop/apocryph/pkg/ethereum"
"github.com/comrade-coop/apocryph/pkg/ipcr"
tpipfs "github.com/comrade-coop/apocryph/pkg/ipfs"
Expand Down Expand Up @@ -99,10 +98,6 @@ var deployPodCmd = &cobra.Command{
configureDeployment(deployment)

if authorize {
encryptionKey, err := tpcrypto.NewKey(tpcrypto.KeyTypeAESGCM256)
if err != nil {
return fmt.Errorf("Could not create AES key: %v", err)
}
// create the keypair that will be accessible for all pods
privateKey, err := ecdsa.GenerateKey(crypto.S256(), rand.Reader)
if err != nil {
Expand All @@ -115,12 +110,9 @@ var deployPodCmd = &cobra.Command{

pubAddress := crypto.PubkeyToAddress(privateKey.PublicKey)

encryptedPrivateKey, err := tpcrypto.EncryptWithKey(encryptionKey, crypto.FromECDSA(privateKey))
if err != nil {
return fmt.Errorf("Could not encrypt private key: %v", err)
}
encodedPrivateKey := ethereum.EncodePrivateKey(crypto.FromECDSA(privateKey))

pod.KeyPair = &pb.KeyPair{Key: encryptionKey, PrivateKey: encryptedPrivateKey, PubAddress: pubAddress.Hex()}
pod.KeyPair = &pb.KeyPair{PrivateKey: encodedPrivateKey, PubAddress: pubAddress.Hex()}
deployment.KeyPair = pod.KeyPair
}

Expand Down
3 changes: 3 additions & 0 deletions pkg/autoscaler/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ func NewAutoSalerServer(ethereumRpc string, p2pHost host.Host) (*AutoScalerServe
log.Printf("ENV Variables: Payment_Address: %v, Publisher Address: %v, ProviderAddress: %v, podId: %v\n", paymentAddress, publisherAddress, providerAddress, podId)

privateKey, err := ethereum.DecodePrivateKey(key)
if err != nil {
return nil, fmt.Errorf("Failed decoding private Key: %v", err)
}

chainID, err := ethClient.ChainID(context.Background())
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions pkg/ethereum/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,8 @@ func GetAccountAndSigner(accountString string, client *ethclient.Client) (*bind.
}
}

func EncodePrivateKey(privateKeyBytes []byte) (string, error) {
return hex.EncodeToString(privateKeyBytes), nil
func EncodePrivateKey(privateKeyBytes []byte) string {
return hex.EncodeToString(privateKeyBytes)
}

func DecodePrivateKey(encodedKey string) (*ecdsa.PrivateKey, error) {
Expand Down
15 changes: 1 addition & 14 deletions pkg/kubernetes/pods.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (
"strings"

"github.com/comrade-coop/apocryph/pkg/constants"
tpcrypto "github.com/comrade-coop/apocryph/pkg/crypto"
"github.com/comrade-coop/apocryph/pkg/ethereum"
pb "github.com/comrade-coop/apocryph/pkg/proto"
"github.com/ethereum/go-ethereum/common"
kedahttpv1alpha1 "github.com/kedacore/http-add-on/operator/apis/http/v1alpha1"
Expand Down Expand Up @@ -116,24 +114,13 @@ func ApplyPodRequest(
}

if podManifest.KeyPair != nil {
privatekey, err := tpcrypto.DecryptWithKey(podManifest.KeyPair.Key, podManifest.KeyPair.PrivateKey)
if err != nil {
return fmt.Errorf("Failed Decrypting private key: %v\n", err)
}

key, err := ethereum.EncodePrivateKey(privatekey)
if err != nil {
return fmt.Errorf("Failed encoding private key: %v\n", err)
}

containerSpec.Env = append(containerSpec.Env, corev1.EnvVar{Name: constants.PRIVATE_KEY, Value: key})
// save as hex to parse later as hex
containerSpec.Env = append(containerSpec.Env, corev1.EnvVar{Name: constants.PAYMENT_ADDR_KEY, Value: common.BytesToAddress(paymentChannel.ContractAddress).Hex()})
containerSpec.Env = append(containerSpec.Env, corev1.EnvVar{Name: constants.PUBLISHER_ADDR_KEY, Value: common.BytesToAddress(paymentChannel.PublisherAddress).Hex()})
containerSpec.Env = append(containerSpec.Env, corev1.EnvVar{Name: constants.PROVIDER_ADDR_KEY, Value: common.BytesToAddress(paymentChannel.ProviderAddress).Hex()})
containerSpec.Env = append(containerSpec.Env, corev1.EnvVar{Name: constants.POD_ID_KEY, Value: common.BytesToHash(paymentChannel.PodID).Hex()})
containerSpec.Env = append(containerSpec.Env, corev1.EnvVar{Name: constants.PRIVATE_KEY, Value: key})
containerSpec.Env = append(containerSpec.Env, corev1.EnvVar{Name: constants.PUBLIC_ADDRESS_KEY, Value: podManifest.KeyPair.PubAddress})
containerSpec.Env = append(containerSpec.Env, corev1.EnvVar{Name: constants.PRIVATE_KEY, Value: podManifest.KeyPair.PrivateKey})
}

for field, value := range container.Env {
Expand Down
12 changes: 3 additions & 9 deletions pkg/proto-ts/pod_pb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -684,14 +684,9 @@ export class Key extends Message<Key> {
*/
export class KeyPair extends Message<KeyPair> {
/**
* @generated from field: apocryph.proto.v0.pod.Key key = 1;
* @generated from field: string privateKey = 2;
*/
key?: Key;

/**
* @generated from field: bytes privateKey = 2;
*/
privateKey = new Uint8Array(0);
privateKey = "";

/**
* @generated from field: string pubAddress = 3;
Expand All @@ -706,8 +701,7 @@ export class KeyPair extends Message<KeyPair> {
static readonly runtime: typeof proto3 = proto3;
static readonly typeName = "apocryph.proto.v0.pod.KeyPair";
static readonly fields: FieldList = proto3.util.newFieldList(() => [
{ no: 1, name: "key", kind: "message", T: Key },
{ no: 2, name: "privateKey", kind: "scalar", T: 12 /* ScalarType.BYTES */ },
{ no: 2, name: "privateKey", kind: "scalar", T: 9 /* ScalarType.STRING */ },
{ no: 3, name: "pubAddress", kind: "scalar", T: 9 /* ScalarType.STRING */ },
]);

Expand Down
52 changes: 20 additions & 32 deletions pkg/proto/pod.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions proto/pod.proto
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ message Key {
}

message KeyPair {
Key key = 1;
bytes privateKey= 2;
string privateKey= 2;
string pubAddress = 3;
}

0 comments on commit 288f792

Please sign in to comment.