Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
gitpod-io
GitHub Repository: gitpod-io/gitpod
Path: blob/main/dev/gpctl/cmd/users.go
2498 views
1
// Copyright (c) 2022 Gitpod GmbH. All rights reserved.
2
// Licensed under the GNU Affero General Public License (AGPL).
3
// See License.AGPL.txt in the project root for license information.
4
5
package cmd
6
7
import (
8
"context"
9
"fmt"
10
"os"
11
12
"github.com/gitpod-io/gitpod/common-go/log"
13
api "github.com/gitpod-io/gitpod/gitpod-protocol"
14
protocol "github.com/gitpod-io/gitpod/gitpod-protocol"
15
"github.com/sirupsen/logrus"
16
"github.com/spf13/cobra"
17
)
18
19
var usersCmd = &cobra.Command{
20
Use: "users",
21
Short: "Interact with Public API services",
22
}
23
24
var usersCmdOpts struct {
25
address string
26
insecure bool
27
token string
28
}
29
30
func init() {
31
rootCmd.AddCommand(usersCmd)
32
33
usersCmd.PersistentFlags().StringVar(&usersCmdOpts.address, "address", "wss://gitpod.io/api/v1", "Address of the API endpoint. Must be in the form <host>:<port>.")
34
usersCmd.PersistentFlags().BoolVar(&usersCmdOpts.insecure, "insecure", false, "Disable TLS when making requests against the API. For testing purposes only.")
35
usersCmd.PersistentFlags().StringVar(&usersCmdOpts.token, "token", os.Getenv("GPCTL_API_TOKEN"), "Authentication token to interact with the API")
36
}
37
38
func newLegacyAPIConn() (*api.APIoverJSONRPC, error) {
39
if usersCmdOpts.address == "" {
40
return nil, fmt.Errorf("empty connection address")
41
}
42
43
if usersCmdOpts.token == "" {
44
return nil, fmt.Errorf("empty connection token. Use --token or GPCTL_API_TOKEN to provide one.")
45
}
46
47
conn, err := api.ConnectToServer(usersCmdOpts.address, api.ConnectToServerOpts{
48
Token: usersCmdOpts.token,
49
Log: logrus.NewEntry(log.Log.Logger),
50
ExtraHeaders: map[string]string{
51
"User-Agent": "gitpod/gpctl",
52
"X-Client-Version": "0",
53
},
54
})
55
if err != nil {
56
return nil, fmt.Errorf("cannot connect to server at %s: %w", usersCmdOpts.address, err)
57
}
58
59
return conn, nil
60
}
61
62
func blockUser(ctx context.Context, args []string, block bool) {
63
client, err := newLegacyAPIConn()
64
if err != nil {
65
log.WithError(err).Fatal("cannot connect")
66
}
67
defer client.Close()
68
69
for _, uid := range args {
70
err = client.AdminBlockUser(ctx, &protocol.AdminBlockUserRequest{
71
UserID: uid,
72
IsBlocked: block,
73
})
74
if err != nil {
75
log.WithField("uid", uid).WithField("block", block).Errorf("AdminBlockUser failed with: %v", err)
76
return
77
} else {
78
log.WithField("uid", uid).WithField("block", block).Info("AdminBlockUser")
79
return
80
}
81
}
82
log.Fatal("no args")
83
}
84
85
func verifyUser(ctx context.Context, args []string) {
86
client, err := newLegacyAPIConn()
87
if err != nil {
88
log.WithError(err).Fatal("cannot connect")
89
}
90
defer client.Close()
91
92
for _, uid := range args {
93
err = client.AdminVerifyUser(ctx, uid)
94
if err != nil {
95
log.WithField("uid", uid).Errorf("AdminVerifyUser failed with: %v", err)
96
return
97
} else {
98
log.WithField("uid", uid).Info("AdminVerifyUser")
99
return
100
}
101
}
102
log.Fatal("no args")
103
}
104
105