summaryrefslogtreecommitdiff
path: root/go/internal/gitlabnet/authorizedkeys/client.go
diff options
context:
space:
mode:
authorPatrick Bajao <ebajao@gitlab.com>2019-08-08 15:49:09 +0800
committerPatrick Bajao <ebajao@gitlab.com>2019-08-08 15:49:09 +0800
commit570ad65f9f4567428ee5214a470a1f97146d58c8 (patch)
treedc01da9252c0acd37966fb53f10a1adbf5e0adf6 /go/internal/gitlabnet/authorizedkeys/client.go
parentc577eb9ed8bd0336870f7a83302f70821d510169 (diff)
downloadgitlab-shell-570ad65f9f4567428ee5214a470a1f97146d58c8.tar.gz
Implement AuthorizedKeys command181-authorized-keys-check-go
Build this command when `Executable` name is `gitlab-shell-authorized-keys-check`. Feature flag is the same name.
Diffstat (limited to 'go/internal/gitlabnet/authorizedkeys/client.go')
-rw-r--r--go/internal/gitlabnet/authorizedkeys/client.go65
1 files changed, 65 insertions, 0 deletions
diff --git a/go/internal/gitlabnet/authorizedkeys/client.go b/go/internal/gitlabnet/authorizedkeys/client.go
new file mode 100644
index 0000000..28b85fc
--- /dev/null
+++ b/go/internal/gitlabnet/authorizedkeys/client.go
@@ -0,0 +1,65 @@
+package authorizedkeys
+
+import (
+ "fmt"
+ "net/url"
+
+ "gitlab.com/gitlab-org/gitlab-shell/go/internal/config"
+ "gitlab.com/gitlab-org/gitlab-shell/go/internal/gitlabnet"
+)
+
+const (
+ AuthorizedKeysPath = "/authorized_keys"
+)
+
+type Client struct {
+ config *config.Config
+ client *gitlabnet.GitlabClient
+}
+
+type Response struct {
+ Id int64 `json:"id"`
+ Key string `json:"key"`
+}
+
+func NewClient(config *config.Config) (*Client, error) {
+ client, err := gitlabnet.GetClient(config)
+ if err != nil {
+ return nil, fmt.Errorf("Error creating http client: %v", err)
+ }
+
+ return &Client{config: config, client: client}, nil
+}
+
+func (c *Client) GetByKey(key string) (*Response, error) {
+ path, err := pathWithKey(key)
+ if err != nil {
+ return nil, err
+ }
+
+ response, err := c.client.Get(path)
+ if err != nil {
+ return nil, err
+ }
+ defer response.Body.Close()
+
+ parsedResponse := &Response{}
+ if err := gitlabnet.ParseJSON(response, parsedResponse); err != nil {
+ return nil, err
+ }
+
+ return parsedResponse, nil
+}
+
+func pathWithKey(key string) (string, error) {
+ u, err := url.Parse(AuthorizedKeysPath)
+ if err != nil {
+ return "", err
+ }
+
+ params := u.Query()
+ params.Set("key", key)
+ u.RawQuery = params.Encode()
+
+ return u.String(), nil
+}