Skip to content

Commit

Permalink
add ability to set display and email to cli
Browse files Browse the repository at this point in the history
Signed-off-by: Kristoffer Dalby <[email protected]>
  • Loading branch information
kradalby committed Dec 18, 2024
1 parent 81e4333 commit 0edd3ae
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 47 deletions.
6 changes: 3 additions & 3 deletions cmd/headscale/cli/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,21 @@ func init() {
enableRouteCmd.Flags().Uint64P("route", "r", 0, "Route identifier (ID)")
err := enableRouteCmd.MarkFlagRequired("route")
if err != nil {
log.Fatalf(err.Error())
log.Fatal(err.Error())
}
routesCmd.AddCommand(enableRouteCmd)

disableRouteCmd.Flags().Uint64P("route", "r", 0, "Route identifier (ID)")
err = disableRouteCmd.MarkFlagRequired("route")
if err != nil {
log.Fatalf(err.Error())
log.Fatal(err.Error())
}
routesCmd.AddCommand(disableRouteCmd)

deleteRouteCmd.Flags().Uint64P("route", "r", 0, "Route identifier (ID)")
err = deleteRouteCmd.MarkFlagRequired("route")
if err != nil {
log.Fatalf(err.Error())
log.Fatal(err.Error())
}
routesCmd.AddCommand(deleteRouteCmd)
}
Expand Down
26 changes: 26 additions & 0 deletions cmd/headscale/cli/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cli
import (
"errors"
"fmt"
"net/url"

survey "github.com/AlecAivazis/survey/v2"
v1 "github.com/juanfont/headscale/gen/go/headscale/v1"
Expand Down Expand Up @@ -40,6 +41,9 @@ func usernameAndIDFromFlag(cmd *cobra.Command) (uint64, string) {
func init() {
rootCmd.AddCommand(userCmd)
userCmd.AddCommand(createUserCmd)
userCmd.Flags().StringP("display-name", "d", "", "Display name")
userCmd.Flags().StringP("email", "e", "", "Email")
userCmd.Flags().StringP("picture-url", "p", "", "Profile picture URL")
userCmd.AddCommand(listUsersCmd)
usernameAndIDFlag(listUsersCmd)
listUsersCmd.Flags().StringP("email", "e", "", "Email")
Expand Down Expand Up @@ -83,6 +87,28 @@ var createUserCmd = &cobra.Command{

request := &v1.CreateUserRequest{Name: userName}

if displayName, _ := cmd.Flags().GetString("display-name"); displayName != "" {
request.DisplayName = displayName
}

if email, _ := cmd.Flags().GetString("email"); email != "" {
request.Email = email
}

if pictureURL, _ := cmd.Flags().GetString("picture-url"); pictureURL != "" {
if _, err := url.Parse(pictureURL); err != nil {
ErrorOutput(
err,
fmt.Sprintf(
"Invalid Picture URL: %s",
err,
),
output,
)
}
request.PictureUrl = pictureURL
}

log.Trace().Interface("request", request).Msg("Sending CreateUser request")
response, err := client.CreateUser(ctx, request)
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions hscontrol/db/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,9 +278,9 @@ func TestConstraints(t *testing.T) {
{
name: "no-duplicate-username-if-no-oidc",
run: func(t *testing.T, db *gorm.DB) {
_, err := CreateUser(db, "user1")
_, err := CreateUser(db, types.User{Name: "user1"})
require.NoError(t, err)
_, err = CreateUser(db, "user1")
_, err = CreateUser(db, types.User{Name: "user1"})
requireConstraintFailed(t, err)
},
},
Expand Down Expand Up @@ -331,7 +331,7 @@ func TestConstraints(t *testing.T) {
{
name: "allow-duplicate-username-cli-then-oidc",
run: func(t *testing.T, db *gorm.DB) {
_, err := CreateUser(db, "user1") // Create CLI username
_, err := CreateUser(db, types.User{Name: "user1"}) // Create CLI username
require.NoError(t, err)

user := types.User{
Expand All @@ -354,7 +354,7 @@ func TestConstraints(t *testing.T) {
err := db.Save(&user).Error
require.NoError(t, err)

_, err = CreateUser(db, "user1") // Create CLI username
_, err = CreateUser(db, types.User{Name: "user1"}) // Create CLI username
require.NoError(t, err)
},
},
Expand Down
24 changes: 12 additions & 12 deletions hscontrol/db/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
)

func (s *Suite) TestGetNode(c *check.C) {
user, err := db.CreateUser("test")
user, err := db.CreateUser(types.User{Name: "test"})
c.Assert(err, check.IsNil)

pak, err := db.CreatePreAuthKey(types.UserID(user.ID), false, false, nil, nil)
Expand Down Expand Up @@ -56,7 +56,7 @@ func (s *Suite) TestGetNode(c *check.C) {
}

func (s *Suite) TestGetNodeByID(c *check.C) {
user, err := db.CreateUser("test")
user, err := db.CreateUser(types.User{Name: "test"})
c.Assert(err, check.IsNil)

pak, err := db.CreatePreAuthKey(types.UserID(user.ID), false, false, nil, nil)
Expand Down Expand Up @@ -85,7 +85,7 @@ func (s *Suite) TestGetNodeByID(c *check.C) {
}

func (s *Suite) TestGetNodeByAnyNodeKey(c *check.C) {
user, err := db.CreateUser("test")
user, err := db.CreateUser(types.User{Name: "test"})
c.Assert(err, check.IsNil)

pak, err := db.CreatePreAuthKey(types.UserID(user.ID), false, false, nil, nil)
Expand Down Expand Up @@ -116,7 +116,7 @@ func (s *Suite) TestGetNodeByAnyNodeKey(c *check.C) {
}

func (s *Suite) TestHardDeleteNode(c *check.C) {
user, err := db.CreateUser("test")
user, err := db.CreateUser(types.User{Name: "test"})
c.Assert(err, check.IsNil)

nodeKey := key.NewNode()
Expand All @@ -141,7 +141,7 @@ func (s *Suite) TestHardDeleteNode(c *check.C) {
}

func (s *Suite) TestListPeers(c *check.C) {
user, err := db.CreateUser("test")
user, err := db.CreateUser(types.User{Name: "test"})
c.Assert(err, check.IsNil)

pak, err := db.CreatePreAuthKey(types.UserID(user.ID), false, false, nil, nil)
Expand Down Expand Up @@ -188,7 +188,7 @@ func (s *Suite) TestGetACLFilteredPeers(c *check.C) {
stor := make([]base, 0)

for _, name := range []string{"test", "admin"} {
user, err := db.CreateUser(name)
user, err := db.CreateUser(types.User{Name: name})
c.Assert(err, check.IsNil)
pak, err := db.CreatePreAuthKey(types.UserID(user.ID), false, false, nil, nil)
c.Assert(err, check.IsNil)
Expand Down Expand Up @@ -279,7 +279,7 @@ func (s *Suite) TestGetACLFilteredPeers(c *check.C) {
}

func (s *Suite) TestExpireNode(c *check.C) {
user, err := db.CreateUser("test")
user, err := db.CreateUser(types.User{Name: "test"})
c.Assert(err, check.IsNil)

pak, err := db.CreatePreAuthKey(types.UserID(user.ID), false, false, nil, nil)
Expand Down Expand Up @@ -320,7 +320,7 @@ func (s *Suite) TestExpireNode(c *check.C) {
}

func (s *Suite) TestSetTags(c *check.C) {
user, err := db.CreateUser("test")
user, err := db.CreateUser(types.User{Name: "test"})
c.Assert(err, check.IsNil)

pak, err := db.CreatePreAuthKey(types.UserID(user.ID), false, false, nil, nil)
Expand Down Expand Up @@ -565,7 +565,7 @@ func TestAutoApproveRoutes(t *testing.T) {
require.NoError(t, err)
require.NotNil(t, pol)

user, err := adb.CreateUser("test")
user, err := adb.CreateUser(types.User{Name: "test"})
require.NoError(t, err)

pak, err := adb.CreatePreAuthKey(types.UserID(user.ID), false, false, nil, nil)
Expand Down Expand Up @@ -706,7 +706,7 @@ func TestListEphemeralNodes(t *testing.T) {
t.Fatalf("creating db: %s", err)
}

user, err := db.CreateUser("test")
user, err := db.CreateUser(types.User{Name: "test"})
require.NoError(t, err)

pak, err := db.CreatePreAuthKey(types.UserID(user.ID), false, false, nil, nil)
Expand Down Expand Up @@ -762,10 +762,10 @@ func TestRenameNode(t *testing.T) {
t.Fatalf("creating db: %s", err)
}

user, err := db.CreateUser("test")
user, err := db.CreateUser(types.User{Name: "test"})
require.NoError(t, err)

user2, err := db.CreateUser("test2")
user2, err := db.CreateUser(types.User{Name: "user2"})
require.NoError(t, err)

node := types.Node{
Expand Down
18 changes: 9 additions & 9 deletions hscontrol/db/preauth_keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func (*Suite) TestCreatePreAuthKey(c *check.C) {
_, err := db.CreatePreAuthKey(12345, true, false, nil, nil)
c.Assert(err, check.NotNil)

user, err := db.CreateUser("test")
user, err := db.CreateUser(types.User{Name: "test"})
c.Assert(err, check.IsNil)

key, err := db.CreatePreAuthKey(types.UserID(user.ID), true, false, nil, nil)
Expand All @@ -41,7 +41,7 @@ func (*Suite) TestCreatePreAuthKey(c *check.C) {
}

func (*Suite) TestExpiredPreAuthKey(c *check.C) {
user, err := db.CreateUser("test2")
user, err := db.CreateUser(types.User{Name: "test2"})
c.Assert(err, check.IsNil)

now := time.Now().Add(-5 * time.Second)
Expand All @@ -60,7 +60,7 @@ func (*Suite) TestPreAuthKeyDoesNotExist(c *check.C) {
}

func (*Suite) TestValidateKeyOk(c *check.C) {
user, err := db.CreateUser("test3")
user, err := db.CreateUser(types.User{Name: "test3"})
c.Assert(err, check.IsNil)

pak, err := db.CreatePreAuthKey(types.UserID(user.ID), true, false, nil, nil)
Expand All @@ -72,7 +72,7 @@ func (*Suite) TestValidateKeyOk(c *check.C) {
}

func (*Suite) TestAlreadyUsedKey(c *check.C) {
user, err := db.CreateUser("test4")
user, err := db.CreateUser(types.User{Name: "test4"})
c.Assert(err, check.IsNil)

pak, err := db.CreatePreAuthKey(types.UserID(user.ID), false, false, nil, nil)
Expand All @@ -94,7 +94,7 @@ func (*Suite) TestAlreadyUsedKey(c *check.C) {
}

func (*Suite) TestReusableBeingUsedKey(c *check.C) {
user, err := db.CreateUser("test5")
user, err := db.CreateUser(types.User{Name: "test5"})
c.Assert(err, check.IsNil)

pak, err := db.CreatePreAuthKey(types.UserID(user.ID), true, false, nil, nil)
Expand All @@ -116,7 +116,7 @@ func (*Suite) TestReusableBeingUsedKey(c *check.C) {
}

func (*Suite) TestNotReusableNotBeingUsedKey(c *check.C) {
user, err := db.CreateUser("test6")
user, err := db.CreateUser(types.User{Name: "test6"})
c.Assert(err, check.IsNil)

pak, err := db.CreatePreAuthKey(types.UserID(user.ID), false, false, nil, nil)
Expand All @@ -128,7 +128,7 @@ func (*Suite) TestNotReusableNotBeingUsedKey(c *check.C) {
}

func (*Suite) TestExpirePreauthKey(c *check.C) {
user, err := db.CreateUser("test3")
user, err := db.CreateUser(types.User{Name: "test3"})
c.Assert(err, check.IsNil)

pak, err := db.CreatePreAuthKey(types.UserID(user.ID), true, false, nil, nil)
Expand All @@ -145,7 +145,7 @@ func (*Suite) TestExpirePreauthKey(c *check.C) {
}

func (*Suite) TestNotReusableMarkedAsUsed(c *check.C) {
user, err := db.CreateUser("test6")
user, err := db.CreateUser(types.User{Name: "test6"})
c.Assert(err, check.IsNil)

pak, err := db.CreatePreAuthKey(types.UserID(user.ID), false, false, nil, nil)
Expand All @@ -158,7 +158,7 @@ func (*Suite) TestNotReusableMarkedAsUsed(c *check.C) {
}

func (*Suite) TestPreAuthKeyACLTags(c *check.C) {
user, err := db.CreateUser("test8")
user, err := db.CreateUser(types.User{Name: "test8"})
c.Assert(err, check.IsNil)

_, err = db.CreatePreAuthKey(types.UserID(user.ID), false, false, nil, []string{"badtag"})
Expand Down
8 changes: 4 additions & 4 deletions hscontrol/db/routes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ var mp = func(p string) netip.Prefix {
}

func (s *Suite) TestGetRoutes(c *check.C) {
user, err := db.CreateUser("test")
user, err := db.CreateUser(types.User{Name: "test"})
c.Assert(err, check.IsNil)

pak, err := db.CreatePreAuthKey(types.UserID(user.ID), false, false, nil, nil)
Expand Down Expand Up @@ -76,7 +76,7 @@ func (s *Suite) TestGetRoutes(c *check.C) {
}

func (s *Suite) TestGetEnableRoutes(c *check.C) {
user, err := db.CreateUser("test")
user, err := db.CreateUser(types.User{Name: "test"})
c.Assert(err, check.IsNil)

pak, err := db.CreatePreAuthKey(types.UserID(user.ID), false, false, nil, nil)
Expand Down Expand Up @@ -150,7 +150,7 @@ func (s *Suite) TestGetEnableRoutes(c *check.C) {
}

func (s *Suite) TestIsUniquePrefix(c *check.C) {
user, err := db.CreateUser("test")
user, err := db.CreateUser(types.User{Name: "test"})
c.Assert(err, check.IsNil)

pak, err := db.CreatePreAuthKey(types.UserID(user.ID), false, false, nil, nil)
Expand Down Expand Up @@ -231,7 +231,7 @@ func (s *Suite) TestIsUniquePrefix(c *check.C) {
}

func (s *Suite) TestDeleteRoutes(c *check.C) {
user, err := db.CreateUser("test")
user, err := db.CreateUser(types.User{Name: "test"})
c.Assert(err, check.IsNil)

pak, err := db.CreatePreAuthKey(types.UserID(user.ID), false, false, nil, nil)
Expand Down
11 changes: 4 additions & 7 deletions hscontrol/db/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,19 @@ var (
ErrUserStillHasNodes = errors.New("user not empty: node(s) found")
)

func (hsdb *HSDatabase) CreateUser(name string) (*types.User, error) {
func (hsdb *HSDatabase) CreateUser(user types.User) (*types.User, error) {
return Write(hsdb.DB, func(tx *gorm.DB) (*types.User, error) {
return CreateUser(tx, name)
return CreateUser(tx, user)
})
}

// CreateUser creates a new User. Returns error if could not be created
// or another user already exists.
func CreateUser(tx *gorm.DB, name string) (*types.User, error) {
err := util.CheckForFQDNRules(name)
func CreateUser(tx *gorm.DB, user types.User) (*types.User, error) {
err := util.CheckForFQDNRules(user.Name)
if err != nil {
return nil, err
}
user := types.User{
Name: name,
}
if err := tx.Create(&user).Error; err != nil {
return nil, fmt.Errorf("creating user: %w", err)
}
Expand Down
Loading

0 comments on commit 0edd3ae

Please sign in to comment.