Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WiP] Add flag to disable automatic account creation #55

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions cmd/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ var (
serverSSHPort int
serverHealthPort int
serverDataDir string
disableAccounts bool

//ServeCmd is the cobra.Command to self-host the Charm Cloud.
ServeCmd = &cobra.Command{
Expand All @@ -41,6 +42,7 @@ var (
if serverDataDir != "" {
cfg.DataDir = serverDataDir
}
cfg.AutoAccounts = !disableAccounts
sp := filepath.Join(cfg.DataDir, ".ssh")
kp, err := keygen.NewWithWrite(sp, "charm_server", []byte(""), keygen.Ed25519)
if err != nil {
Expand Down Expand Up @@ -73,4 +75,5 @@ func init() {
ServeCmd.Flags().IntVar(&serverSSHPort, "ssh-port", 0, "SSH port to listen on")
ServeCmd.Flags().IntVar(&serverHealthPort, "health-port", 0, "Health port to listen on")
ServeCmd.Flags().StringVar(&serverDataDir, "data-dir", "", "Directory to store SQLite db, SSH keys and file data")
ServeCmd.Flags().BoolVar(&disableAccounts, "disable-accounts", false, "Create accounts for new users automatically if they don't exist")
}
3 changes: 2 additions & 1 deletion proto/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package proto

import (
"errors"
"fmt"
)

// ErrMalformedKey parsing error for bad ssh key.
Expand Down Expand Up @@ -37,7 +38,7 @@ type ErrAuthFailed struct {
}

// Error returns the boxed error string.
func (e ErrAuthFailed) Error() string { return e.Err.Error() }
func (e ErrAuthFailed) Error() string { return fmt.Sprintf("authentication failed: %s", e.Err.Error()) }
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tries to make it more clear it's an auth related errror.


// Unwrap returns the boxed error.
func (e ErrAuthFailed) Unwrap() error { return e.Err }
6 changes: 3 additions & 3 deletions server/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (me *SSHServer) handleAPIAuth(s ssh.Session) {
me.errorLog.Println(err)
return
}
u, err := me.db.UserForKey(key, true)
u, err := me.db.UserForKey(key, me.config.AutoAccounts)
if err != nil {
me.errorLog.Println(err)
return
Expand Down Expand Up @@ -85,7 +85,7 @@ func (me *SSHServer) handleAPIKeys(s ssh.Session) {
_ = me.sendAPIMessage(s, "Missing key")
return
}
u, err := me.db.UserForKey(key, true)
u, err := me.db.UserForKey(key, me.config.AutoAccounts)
if err != nil {
me.errorLog.Println(err)
_ = me.sendAPIMessage(s, fmt.Sprintf("API keys error: %s", err))
Expand Down Expand Up @@ -121,7 +121,7 @@ func (me *SSHServer) handleID(s ssh.Session) {
me.errorLog.Println(err)
return
}
u, err := me.db.UserForKey(key, true)
u, err := me.db.UserForKey(key, me.config.AutoAccounts)
if err != nil {
me.errorLog.Println(err)
return
Expand Down
4 changes: 2 additions & 2 deletions server/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ func (me *SSHServer) LinkGen(lt charm.LinkTransport) error {
if u.CharmID == "" {
// Create account for the link generator public key if it doesn't exist
log.Printf("Creating account for token: %s", tok)
u, err = me.db.UserForKey(u.PublicKey.Key, true)
u, err = me.db.UserForKey(u.PublicKey.Key, me.config.AutoAccounts)
if err != nil {
log.Printf("Create account error: %s", err)
l.Status = charm.LinkStatusError
Expand Down Expand Up @@ -272,7 +272,7 @@ func (me *SSHServer) handleLinkGenAPI(s ssh.Session) {
_ = me.sendAPIMessage(s, fmt.Sprintf("Missing public key %s", err))
return
}
u, err := me.db.UserForKey(key, true)
u, err := me.db.UserForKey(key, me.config.AutoAccounts)
if err != nil {
_ = me.sendAPIMessage(s, fmt.Sprintf("Storage key lookup error: %s", err))
return
Expand Down
35 changes: 18 additions & 17 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,24 @@ import (

// Config is the configuration for the Charm server.
type Config struct {
Host string `env:"CHARM_SERVER_HOST"`
SSHPort int `env:"CHARM_SERVER_SSH_PORT" default:"35353"`
HTTPPort int `env:"CHARM_SERVER_HTTP_PORT" default:"35354"`
StatsPort int `env:"CHARM_SERVER_STATS_PORT" default:"35355"`
HealthPort int `env:"CHARM_SERVER_HEALTH_PORT" default:"35356"`
DataDir string `env:"CHARM_SERVER_DATA_DIR" default:"./data"`
TLSKeyFile string `env:"CHARM_SERVER_TLS_KEY_FILE"`
TLSCertFile string `env:"CHARM_SERVER_TLS_CERT_FILE"`
errorLog *log.Logger
PublicKey []byte
PrivateKey []byte
DB db.DB
FileStore storage.FileStore
Stats stats.Stats
tlsConfig *tls.Config
jwtKeyPair JSONWebKeyPair
httpScheme string
Host string `env:"CHARM_SERVER_HOST"`
SSHPort int `env:"CHARM_SERVER_SSH_PORT" default:"35353"`
HTTPPort int `env:"CHARM_SERVER_HTTP_PORT" default:"35354"`
StatsPort int `env:"CHARM_SERVER_STATS_PORT" default:"35355"`
HealthPort int `env:"CHARM_SERVER_HEALTH_PORT" default:"35356"`
DataDir string `env:"CHARM_SERVER_DATA_DIR" default:"./data"`
TLSKeyFile string `env:"CHARM_SERVER_TLS_KEY_FILE"`
TLSCertFile string `env:"CHARM_SERVER_TLS_CERT_FILE"`
errorLog *log.Logger
PublicKey []byte
PrivateKey []byte
DB db.DB
FileStore storage.FileStore
Stats stats.Stats
tlsConfig *tls.Config
jwtKeyPair JSONWebKeyPair
httpScheme string
AutoAccounts bool `env:"CHARM_SERVER_AUTO_ACCOUNTS" default:"true"`
}

// Server contains the SSH and HTTP servers required to host the Charm Cloud.
Expand Down