Skip to content

Commit

Permalink
nex testing
Browse files Browse the repository at this point in the history
  • Loading branch information
hooksie1 committed Sep 26, 2024
1 parent 6a1fc81 commit 41f4043
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 38 deletions.
16 changes: 16 additions & 0 deletions appspec.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "piggybank-nex",
"version": "0.1.1",
"description": "A secrets storage tool",
"components": [
{
"name": "piggybank-service",
"workload_type": "native",
"placement_policy": {
"policy_name": "fixed_scale",
"instances": 1,
"regions": []
}
}
]
}
8 changes: 3 additions & 5 deletions cmd/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@ import (

// bindNatsFlags binds nats flag values to viper
func bindNatsFlags(cmd *cobra.Command) {
viper.BindPFlag("nats_urls", cmd.Flags().Lookup("nats-urls"))
viper.BindPFlag("nats_seed", cmd.Flags().Lookup("nats-seed"))
viper.BindPFlag("nats_jwt", cmd.Flags().Lookup("nats-jwt"))
viper.BindPFlag("nats_server", cmd.Flags().Lookup("nats-urls"))
viper.BindPFlag("nats_user_seed", cmd.Flags().Lookup("nats-seed"))
viper.BindPFlag("nats_user_jwt", cmd.Flags().Lookup("nats-jwt"))
viper.BindPFlag("nats_secret", cmd.Flags().Lookup("nats-secret"))
viper.BindPFlag("credentials_file", cmd.Flags().Lookup("credentials-file"))
viper.BindPFlag("use_traffic_shaping", cmd.Flags().Lookup("use-traffic-shaping"))
}

// natsFlags adds the nats flags to the passed in cobra command
Expand All @@ -28,7 +27,6 @@ func natsFlags(cmd *cobra.Command) {
cmd.PersistentFlags().String("nats-seed", "", "NATS seed as a string")
cmd.PersistentFlags().String("credentials-file", "", "Path to NATS user credentials file")
cmd.PersistentFlags().String("nats-urls", "nats://localhost:4222", "NATS URLs")
cmd.PersistentFlags().Bool("use-traffic-shaping", false, "Local development connection")
}

func bindClientFlags(cmd *cobra.Command) {
Expand Down
13 changes: 5 additions & 8 deletions cmd/nats.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package cmd

import (
"os"

"github.com/CoverWhale/logr"
"github.com/nats-io/jsm.go/natscontext"
"github.com/nats-io/nats.go"
Expand All @@ -21,19 +19,18 @@ func newNatsConnection(connOpts natsOpts) (*nats.Conn, error) {
opts = append(opts, nats.CustomInboxPrefix(connOpts.prefix))
}

_, ok := os.LookupEnv("USER")

if viper.GetString("credentials_file") == "" && viper.GetString("nats_jwt") == "" && ok {
if viper.GetString("credentials_file") == "" && viper.GetString("nats_user_jwt") == "" {
logr.Debug("using NATS context")
return natscontext.Connect("", opts...)
}

if viper.GetString("nats_jwt") != "" {
opts = append(opts, nats.UserJWTAndSeed(viper.GetString("nats_jwt"), viper.GetString("nats_seed")))
if viper.GetString("nats_user_jwt") != "" {
logr.Debug("using env creds")
opts = append(opts, nats.UserJWTAndSeed(viper.GetString("nats_user_jwt"), viper.GetString("nats_user_seed")))
}
if viper.GetString("credentials_file") != "" {
opts = append(opts, nats.UserCredentials(viper.GetString("credentials_file")))
}

return nats.Connect(viper.GetString("nats_urls"), opts...)
return nats.Connect(viper.GetString("nats_server"), opts...)
}
24 changes: 3 additions & 21 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ var cfg Config
var rootCmd = &cobra.Command{
Use: "piggybankctl",
Short: "The app description",
RunE: start,
}
var replacer = strings.NewReplacer("-", "_")

Expand All @@ -32,34 +33,15 @@ func Execute() {

func init() {
cobra.OnInitialize(initConfig)
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.piggybank.json)")
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}

func initConfig() {

if cfgFile != "" {
viper.SetConfigFile(cfgFile)
} else {
home, err := os.UserHomeDir()
cobra.CheckErr(err)

viper.AddConfigPath(home)
viper.SetConfigType("json")
viper.SetConfigName(".piggybank")
}

viper.SetEnvPrefix("piggybank")
viper.SetEnvPrefix("nex_hostservices")
viper.AutomaticEnv()
viper.SetEnvKeyReplacer(replacer)

// If a config file is found, read it in.
logger := logr.NewLogger()
if err := viper.ReadInConfig(); err == nil {
logger.Debugf("using config %s", viper.ConfigFileUsed())
}

if err := viper.Unmarshal(&cfg); err != nil {
cobra.CheckErr(err)
}
logger.Debug("initialized")
}
2 changes: 1 addition & 1 deletion cmd/secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func init() {
}

func getSubject(verb string, id string) string {
return fmt.Sprintf("piggybank.secrets.%s.%s", strings.ToUpper(verb), id)
return fmt.Sprintf("nex.piggybank.secrets.%s.%s", strings.ToUpper(verb), id)
}

func secrets(cmd *cobra.Command, args []string) error {
Expand Down
4 changes: 2 additions & 2 deletions service/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

const (
databaseSubject = "piggybank.database"
databaseSubject = "nex.piggybank.database"
databaseInitSubject = "initialize"
databaseUnlockSubject = "unlock"
databaseLockSubject = "lock"
Expand All @@ -23,7 +23,7 @@ const (
GET Verb = "GET"
POST Verb = "POST"
DELETE Verb = "DELETE"
secretSubject = "piggybank.secrets"
secretSubject = "nex.piggybank.secrets"
)

var SubjectVerbs = map[DBVerb]string{
Expand Down
2 changes: 1 addition & 1 deletion service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func DBGroup(svc micro.Service, logger *logr.Logger, appCtx AppContext) {
}

func AppGroup(svc micro.Service, logger *logr.Logger, appCtx AppContext) {
appGroup := svc.AddGroup("piggybank.secrets", micro.WithGroupQueueGroup("app"))
appGroup := svc.AddGroup(secretSubject, micro.WithGroupQueueGroup("app"))
appGroup.AddEndpoint("GET",
AppHandler(logger, SecretHandler(GetRecord), appCtx),
micro.WithEndpointMetadata(map[string]string{
Expand Down

0 comments on commit 41f4043

Please sign in to comment.