-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create ./pkg for different source packages. Seperate code out into model, slosilo, server and utils packages. Introduce conjurctl CLI cmd as the common interface for consuming this application.
- Loading branch information
1 parent
a955f7b
commit 12fb1a6
Showing
23 changed files
with
1,017 additions
and
430 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,35 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/spf13/cobra" | ||
) | ||
|
||
// dataKeyCmd represents the data-key command | ||
var dataKeyCmd = &cobra.Command{ | ||
Use: "data-key", | ||
Short: "Manage the data encryption key", | ||
Long: `Manage the data encryption key`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
fmt.Println("error: Command 'data-key' requires a subcommand generate") | ||
fmt.Println() | ||
cmd.Help() | ||
os.Exit(1) | ||
}, | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(dataKeyCmd) | ||
|
||
// Here you will define your flags and configuration settings. | ||
|
||
// Cobra supports Persistent Flags which will work for this command | ||
// and all subcommands, e.g.: | ||
// dataKeyCmd.PersistentFlags().String("foo", "", "A help for foo") | ||
|
||
// Cobra supports local flags which will only run when this command | ||
// is called directly, e.g.: | ||
// dataKeyCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") | ||
} |
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,34 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/base64" | ||
"fmt" | ||
|
||
"github.com/spf13/cobra" | ||
|
||
"conjur-in-go/pkg/slosilo" | ||
) | ||
|
||
// dataKeyGenerateCmd represents the data-key > generate command | ||
var dataKeyGenerateCmd = &cobra.Command{ | ||
Use: "generate", | ||
Short: "Generate a data encryption key", | ||
Long: ` | ||
Generate a data encryption key | ||
Use this command to generate a new Base64-encoded 256 bit data encryption key. Once generated, this key should be placed into the environment of | ||
the Conjur server. It will be used to encrypt all sensitive data which is stored in the database, including the token-signing private key. | ||
Example: | ||
$ export CONJUR_DATA_KEY="$(conjurctl data-key generate)" | ||
`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
bytes, _ := slosilo.RandomBytes(32) | ||
fmt.Printf("%s", base64.StdEncoding.Strict().EncodeToString(bytes)) | ||
}, | ||
} | ||
|
||
func init() { | ||
dataKeyCmd.AddCommand(dataKeyGenerateCmd) | ||
} |
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,5 @@ | ||
package main | ||
|
||
func main() { | ||
Execute() | ||
} |
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,60 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
//var cfgFile string | ||
|
||
// rootCmd represents the base command when called without any subcommands | ||
var rootCmd = &cobra.Command{ | ||
Use: "conjurctl", | ||
Short: "Command and control application for Conjur", | ||
Long: `Command and control application for Conjur`, | ||
// Uncomment the following line if your bare application | ||
// has an action associated with it: | ||
// Run: func(cmd *cobra.Command, args []string) { }, | ||
} | ||
|
||
// Execute adds all child commands to the root command and sets flags appropriately. | ||
// This is called by main.main(). It only needs to happen once to the rootCmd. | ||
func Execute() { | ||
cobra.CheckErr(rootCmd.Execute()) | ||
} | ||
|
||
func init() { | ||
cobra.OnInitialize(initConfig) | ||
|
||
// Here you will define your flags and configuration settings. | ||
// Cobra supports persistent flags, which, if defined here, | ||
// will be global for your application. | ||
|
||
//rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.conjurctl.yaml)") | ||
|
||
// Cobra also supports local flags, which will only run | ||
// when this action is called directly. | ||
//rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") | ||
} | ||
|
||
// initConfig reads in config file and ENV variables if set. | ||
func initConfig() { | ||
//if cfgFile != "" { | ||
// // Use config file from the flag. | ||
// viper.SetConfigFile(cfgFile) | ||
//} else { | ||
// // Find home directory. | ||
// home, err := homedir.Dir() | ||
// cobra.CheckErr(err) | ||
// | ||
// // Search config in home directory with name ".conjurctl" (without extension). | ||
// viper.AddConfigPath(home) | ||
// viper.SetConfigName(".conjurctl") | ||
//} | ||
// | ||
//viper.AutomaticEnv() // read in environment variables that match | ||
// | ||
//// If a config file is found, read it in. | ||
//if err := viper.ReadInConfig(); err == nil { | ||
// fmt.Fprintln(os.Stderr, "Using config file:", viper.ConfigFileUsed()) | ||
//} | ||
} |
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,91 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/base64" | ||
"fmt" | ||
"log" | ||
"os" | ||
|
||
"github.com/spf13/cobra" | ||
"gorm.io/driver/postgres" | ||
"gorm.io/gorm" | ||
|
||
"conjur-in-go/pkg/server" | ||
"conjur-in-go/pkg/server/endpoints" | ||
"conjur-in-go/pkg/slosilo/store" | ||
) | ||
|
||
|
||
// NOTES | ||
// tokenSigningPrivateKey is stored in slosilo keystore | ||
|
||
const defaultBindAddress = "127.0.0.1" | ||
const defaultPort = "8000" | ||
|
||
// serverCmd represents the server command | ||
var serverCmd = &cobra.Command{ | ||
Use: "server", | ||
Short: "Run the Conjur application server", | ||
Long: `Run the Conjur application server | ||
To run the server requires the environment variables CONJUR_DATA_KEY and DATABASE_URL.`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
dataKeyB64, ok := os.LookupEnv("CONJUR_DATA_KEY") | ||
if !ok { | ||
fmt.Println("No CONJUR_DATA_KEY") | ||
os.Exit(1) | ||
} | ||
|
||
dataKey, err := base64.StdEncoding.DecodeString(dataKeyB64) | ||
if err != nil { | ||
fmt.Println("Bad CONJUR_DATA_KEY:", err) | ||
os.Exit(1) | ||
} | ||
|
||
db, err := gorm.Open( | ||
postgres.New( | ||
postgres.Config{ | ||
DSN: os.Getenv("DATABASE_URL"), | ||
PreferSimpleProtocol: true, // disables implicit prepared statement usage | ||
}, | ||
), | ||
&gorm.Config{ | ||
}, | ||
) | ||
if err != nil { | ||
fmt.Println("Unable to connect to DB:", err) | ||
os.Exit(1) | ||
} | ||
|
||
keystore, err := store.NewKeyStore(db, dataKey) | ||
if err != nil { | ||
fmt.Println("Unable to initiate keystore:", err) | ||
os.Exit(1) | ||
} | ||
|
||
host, _ := cmd.Flags().GetString("bind-address") | ||
port, _ := cmd.Flags().GetString("port") | ||
s := server.NewServer(keystore, db, host, port) | ||
|
||
endpoints.RegisterSecretReadEndpoint(s) | ||
endpoints.RegisterAuthenticateEndpoint(s) | ||
|
||
log.Printf("Running server at http://%s:%s...\n", host, port) | ||
log.Fatal(s.Start()) | ||
}, | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(serverCmd) | ||
|
||
// Here you will define your flags and configuration settings. | ||
|
||
// Cobra supports Persistent Flags which will work for this command | ||
// and all subcommands, e.g.: | ||
// serverCmd.PersistentFlags().String("foo", "", "A help for foo") | ||
|
||
// Cobra supports local flags which will only run when this command | ||
// is called directly, e.g.: | ||
serverCmd.Flags().StringP("port", "p", defaultPort, "server listen port") | ||
serverCmd.Flags().StringP("bind-address", "b", defaultBindAddress, "server bind address") | ||
} |
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.