File: key.go

package info (click to toggle)
golang-github-apparentlymart-go-rundeck-api 0.0.1%2Bgit20170705.2c962ac-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 120 kB
  • sloc: makefile: 2
file content (89 lines) | stat: -rw-r--r-- 3,265 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package rundeck

// KeyMeta is the metadata associated with a resource in the Rundeck key store.
type KeyMeta struct {
	XMLName string `xml:"resource"`
	Name string `xml:"name,attr,omitempty"`
	Path string `xml:"path,attr,omitempty"`
	ResourceType string `xml:"type,attr,omitempty"`
	URL string `xml:"url,attr,omitempty"`
	ContentType string `xml:"resource-meta>Rundeck-content-type"`
	ContentSize string `xml:"resource-meta>Rundeck-content-size"`
	ContentMask string `xml:"resource-meta>Rundeck-content-mask"`
	KeyType string `xml:"resource-meta>Rundeck-key-type"`
	LastModifiedByUserName string `xml:"resource-meta>Rundeck-auth-modified-username"`
	CreatedByUserName string `xml:"resource-meta>Rundeck-auth-created-username"`
	CreatedTimestamp string `xml:"resource-meta>Rundeck-content-creation-time"`
	LastModifiedTimestamp string `xml:"resource-meta>Rundeck-content-modify-time"`
}

type keyMetaListContents struct {
	Keys []KeyMeta `xml:"contents>resource"`
}

// GetKeyMeta returns the metadata for the key at the given keystore path.
func (c *Client) GetKeyMeta(path string) (*KeyMeta, error) {
	k := &KeyMeta{}
	err := c.get([]string{"storage", "keys", path}, nil, k)
	return k, err
}

// GetKeysInDirMeta returns the metadata for the keys and subdirectories within
// the directory at the given keystore path.
func (c *Client) GetKeysInDirMeta(path string) ([]KeyMeta, error) {
	r := &keyMetaListContents{}
	err := c.get([]string{"storage", "keys", path}, nil, r)
	if err != nil {
		return nil, err
	}
	return r.Keys, nil
}

// GetKeyContent retrieves and returns the content of the key at the given keystore path.
// Private keys are write-only, so they cannot be retrieved via this interface.
func (c *Client) GetKeyContent(path string) (string, error) {
	return c.rawGet([]string{"storage", "keys", path}, nil, "application/pgp-keys")
}

func (c *Client) CreatePublicKey(path string, content string) error {
	return c.createOrReplacePublicKey("POST", path, "application/pgp-keys", content)
}

func (c *Client) ReplacePublicKey(path string, content string) error {
	return c.createOrReplacePublicKey("PUT", path, "application/pgp-keys", content)
}

func (c *Client) CreatePrivateKey(path string, content string) error {
	return c.createOrReplacePublicKey("POST", path, "application/octet-stream", content)
}

func (c *Client) ReplacePrivateKey(path string, content string) error {
	return c.createOrReplacePublicKey("PUT", path, "application/octet-stream", content)
}

func (c *Client) CreatePassword(path string, content string) error {
	return c.createOrReplacePublicKey("POST", path, "application/x-rundeck-data-password", content)
}

func (c *Client) ReplacePassword(path string, content string) error {
	return c.createOrReplacePublicKey("PUT", path, "application/x-rundeck-data-password", content)
}

func (c *Client) createOrReplacePublicKey(method string, path string, contentType string, content string) error {
	req := &request{
		Method: method,
		PathParts: []string{"storage", "keys", path},
		Headers: map[string]string{
			"Content-Type": contentType,
		},
		BodyBytes: []byte(content),
	}

	_, err := c.rawRequest(req)

	return err
}

func (c *Client) DeleteKey(path string) error {
	return c.delete([]string{"storage", "keys", path})
}