Skip to content

Commit

Permalink
Optimize unreasonable specifications
Browse files Browse the repository at this point in the history
  • Loading branch information
eric committed Oct 21, 2023
1 parent 0fa75a3 commit 42014a8
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 43 deletions.
60 changes: 29 additions & 31 deletions op-aws-sdk/key_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,62 +3,59 @@ package op_aws_sdk
import (
"encoding/json"
"errors"
"os"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/secretsmanager"
"github.com/ethereum/go-ethereum/log"
"github.com/urfave/cli"
"golang.org/x/net/context"
"os"
)

const (
OP_NODE_P2P_SEQUENCER_KEY = "OP_NODE_P2P_SEQUENCER_KEY"
OP_BATCHER_SIGN_KEY = "OP_BATCHER_SIGN_KEY"
OP_PROPOSER_SIGN_KEY = "OP_PROPOSER_SIGN_KEY"

AWS_KEY_NAME = "pk"
AWS_KEY_JSON_NAME = "pk"
)

func Key_manager(context context.Context, ctx *cli.Context, keyName string) error {

aws_key_id := ""
aws_key_region := ""
key_flag_name := ""
log.Info("Key manager ", "keyName", keyName)
switch keyName {
func KeyManager(context context.Context, ctx *cli.Context, keyType string) error {
secretName := ""
awsRegion := ""
flagName := ""
log.Info("Key manager ", "keyType", keyType)
switch keyType {
case OP_NODE_P2P_SEQUENCER_KEY:
aws_key_id = "AWS_P2P_SEQUENCER_KEY_ID"
aws_key_region = "AWS_P2P_SEQUENCER_KEY_REGION"
key_flag_name = "p2p.sequencer.key"
secretName = "OP_NODE_AWS_P2P_SECRET_NAME"
awsRegion = "OP_NODE_AWS_P2P_SECRET_REGION"
flagName = "p2p.sequencer.key"
case OP_BATCHER_SIGN_KEY:
aws_key_id = "AWS_OP_BATCHER_SIGN_KEY_ID"
aws_key_region = "AWS_OP_BATCHER_SIGN_KEY_REGION"
key_flag_name = "private-key"
secretName = "OP_BATCHER_AWS_SECRET_NAME"
awsRegion = "OP_BATCHER_AWS_SECRET_REGION"
flagName = "private-key"
case OP_PROPOSER_SIGN_KEY:
aws_key_id = "AWS_OP_PROPOSER_SIGN_KEY_ID"
aws_key_region = "AWS_OP_PROPOSER_SIGN_KEY_REGION"
key_flag_name = "private-key"
secretName = "OP_PROPOSER_AWS_SECRET_NAME"
awsRegion = "OP_PROPOSER_AWS_SECRET_REGION"
flagName = "private-key"
default:
log.Error("Key manager ", "error keyName", keyName)
log.Error("Key manager ", "error keyType", keyType)
return nil
}
return load(context, ctx, aws_key_region, aws_key_id, key_flag_name)
return load(context, ctx, awsRegion, secretName, flagName)
}
func load(context context.Context, ctx *cli.Context, aws_key_region string, aws_key_id string, key_flag_name string) error {
log.Info("Key manager load key ", "aws_key_region", aws_key_region, "aws_key_id", aws_key_id)
aws_key_id = os.Getenv(aws_key_id)
aws_key_region = os.Getenv(aws_key_region)
if aws_key_id != "" || aws_key_region != "" {
log.Info("Key manager ", "aws_key_region", aws_key_region, "aws_key_id", aws_key_id)
config, err := config.LoadDefaultConfig(context, config.WithRegion(aws_key_region))
func load(context context.Context, ctx *cli.Context, awsRegion string, secretName string, flagName string) error {
name := os.Getenv(secretName)
region := os.Getenv(awsRegion)
if name != "" {
config, err := config.LoadDefaultConfig(context, config.WithRegion(region))
if err != nil {
log.Error("Key manager load key config from aws", "error", err)
return err
}
secretManager := secretsmanager.NewFromConfig(config)
input := &secretsmanager.GetSecretValueInput{
SecretId: aws.String(aws_key_id),
SecretId: aws.String(name),
VersionStage: aws.String("AWSCURRENT"),
}
result, err := secretManager.GetSecretValue(context, input)
Expand All @@ -72,12 +69,13 @@ func load(context context.Context, ctx *cli.Context, aws_key_region string, aws_
if err != nil {
return err
}
key, ok := resultMap[AWS_KEY_NAME]
key, ok := resultMap[AWS_KEY_JSON_NAME]
if !ok {
log.Error("Key manager load key is not exist")
return errors.New("Key manager load key is not exist")
}
ctx.Set(key_flag_name, key)
log.Info("Key manager load key is success")
ctx.Set(flagName, key)
}
return nil
}
9 changes: 4 additions & 5 deletions op-batcher/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@ package main

import (
"fmt"
op_aws_sdk "github.com/ethereum-optimism/optimism/op-aws-sdk"
"golang.org/x/net/context"
"os"

"github.com/urfave/cli"

op_aws_sdk "github.com/ethereum-optimism/optimism/op-aws-sdk"
"github.com/ethereum-optimism/optimism/op-batcher/batcher"
"github.com/ethereum-optimism/optimism/op-batcher/cmd/doc"
"github.com/ethereum-optimism/optimism/op-batcher/flags"
oplog "github.com/ethereum-optimism/optimism/op-service/log"
"github.com/ethereum/go-ethereum/log"
"github.com/urfave/cli"
"golang.org/x/net/context"
)

var (
Expand Down Expand Up @@ -48,7 +47,7 @@ func main() {
// This is done to capture the Version of the batcher.
func curryMain(version string) func(ctx *cli.Context) error {
return func(ctx *cli.Context) error {
if err := op_aws_sdk.Key_manager(context.Background(), ctx, op_aws_sdk.OP_BATCHER_SIGN_KEY); err != nil {
if err := op_aws_sdk.KeyManager(context.Background(), ctx, op_aws_sdk.OP_BATCHER_SIGN_KEY); err != nil {
return err
}
return batcher.Main(version, ctx)
Expand Down
4 changes: 2 additions & 2 deletions op-node/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"context"
op_aws_sdk "github.com/ethereum-optimism/optimism/op-aws-sdk"
"net"
"os"
"strconv"
Expand All @@ -14,6 +13,7 @@ import (

"github.com/ethereum/go-ethereum/log"

op_aws_sdk "github.com/ethereum-optimism/optimism/op-aws-sdk"
opnode "github.com/ethereum-optimism/optimism/op-node"
"github.com/ethereum-optimism/optimism/op-node/cmd/genesis"
"github.com/ethereum-optimism/optimism/op-node/cmd/p2p"
Expand Down Expand Up @@ -91,7 +91,7 @@ func RollupNodeMain(ctx *cli.Context) error {
log := oplog.NewLogger(logCfg)
opservice.ValidateEnvVars(flags.EnvVarPrefix, flags.Flags, log)
m := metrics.NewMetrics("default")
if err := op_aws_sdk.Key_manager(context.Background(), ctx, op_aws_sdk.OP_NODE_P2P_SEQUENCER_KEY); err != nil {
if err := op_aws_sdk.KeyManager(context.Background(), ctx, op_aws_sdk.OP_NODE_P2P_SEQUENCER_KEY); err != nil {
return err
}
cfg, err := opnode.NewConfig(ctx, log)
Expand Down
9 changes: 4 additions & 5 deletions op-proposer/cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,16 @@ package main

import (
"fmt"
op_aws_sdk "github.com/ethereum-optimism/optimism/op-aws-sdk"
"golang.org/x/net/context"
"os"

"github.com/urfave/cli"

op_aws_sdk "github.com/ethereum-optimism/optimism/op-aws-sdk"
"github.com/ethereum-optimism/optimism/op-proposer/cmd/doc"
"github.com/ethereum-optimism/optimism/op-proposer/flags"
"github.com/ethereum-optimism/optimism/op-proposer/proposer"
oplog "github.com/ethereum-optimism/optimism/op-service/log"
"github.com/ethereum/go-ethereum/log"
"github.com/urfave/cli"
"golang.org/x/net/context"
)

var (
Expand Down Expand Up @@ -48,7 +47,7 @@ func main() {
// This is done to capture the Version of the proposer.
func curryMain(version string) func(ctx *cli.Context) error {
return func(ctx *cli.Context) error {
if err := op_aws_sdk.Key_manager(context.Background(), ctx, op_aws_sdk.OP_PROPOSER_SIGN_KEY); err != nil {
if err := op_aws_sdk.KeyManager(context.Background(), ctx, op_aws_sdk.OP_PROPOSER_SIGN_KEY); err != nil {
return err
}
return proposer.Main(version, ctx)
Expand Down

0 comments on commit 42014a8

Please sign in to comment.