Skip to content

Commit 88e301c

Browse files
committed
feat: refactor credentials entry struct to support more sub types
1 parent 754ae80 commit 88e301c

12 files changed

+551
-336
lines changed

.github/workflows/test.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,6 @@ jobs:
6464
uses: ./.github/workflows/tailscale
6565
with:
6666
auth_key: ${{ secrets.TAILSCALE_AUTH_KEY_EPHEMERAL }}
67-
exit_node: 100.99.49.20
68-
accept_dns: true
6967

7068
- name: Test application
7169
uses: ./.github/workflows/go-test

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.11.0
1+
0.12.0

authentication.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,10 @@ func NewClient(appKey string, appSecret string, baseUri string) (Client, error)
6060
client.common.client = &client
6161

6262
client.Entries = &Entries{
63-
UserCredential: (*EntryUserCredentialService)(&client.common),
64-
Certificate: (*EntryCertificateService)(&client.common),
65-
Website: (*EntryWebsiteService)(&client.common),
66-
Host: (*EntryHostService)(&client.common),
63+
Credential: (*EntryCredentialService)(&client.common),
64+
Certificate: (*EntryCertificateService)(&client.common),
65+
Website: (*EntryWebsiteService)(&client.common),
66+
Host: (*EntryHostService)(&client.common),
6767
}
6868
client.Vaults = (*Vaults)(&client.common)
6969

entries.go

Lines changed: 89 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,110 @@
11
package dvls
22

33
import (
4-
"strconv"
4+
"encoding/json"
5+
"fmt"
56
"strings"
67
)
78

89
const (
910
entryEndpoint string = "/api/connections/partial"
1011
entryConnectionsEndpoint string = "/api/connections"
12+
entryBasePublicEndpoint string = "/api/v1/vault/{vaultId}/entry"
13+
entryPublicEndpoint string = "/api/v1/vault/{vaultId}/entry/{id}"
1114
)
1215

1316
type Entries struct {
14-
Certificate *EntryCertificateService
15-
Host *EntryHostService
16-
UserCredential *EntryUserCredentialService
17-
Website *EntryWebsiteService
18-
}
19-
20-
func keywordsToSlice(kw string) []string {
21-
var spacedTag bool
22-
tags := strings.FieldsFunc(string(kw), func(r rune) bool {
23-
if r == '"' {
24-
spacedTag = !spacedTag
25-
}
26-
return !spacedTag && r == ' '
27-
})
28-
for i, v := range tags {
29-
unquotedTag, err := strconv.Unquote(v)
30-
if err != nil {
31-
continue
32-
}
17+
Certificate *EntryCertificateService
18+
Host *EntryHostService
19+
Credential *EntryCredentialService
20+
Website *EntryWebsiteService
21+
}
22+
23+
type Entry struct {
24+
ID string `json:"id,omitempty"`
25+
VaultId string `json:"vaultId,omitempty"`
26+
Name string `json:"name"`
27+
Path string `json:"path"`
28+
Type string `json:"type"`
29+
SubType string `json:"subType"`
30+
31+
Data EntryData `json:"data,omitempty"`
32+
33+
Description string `json:"description"`
34+
ModifiedBy string `json:"modifiedBy,omitempty"`
35+
ModifiedOn *ServerTime `json:"modifiedOn,omitempty"`
36+
CreatedBy string `json:"createdBy,omitempty"`
37+
CreatedOn *ServerTime `json:"createdOn,omitempty"`
38+
Tags []string `json:"tags,omitempty"`
39+
}
40+
41+
type EntryData any
42+
43+
func (e *Entry) GetType() string {
44+
return e.Type
45+
}
46+
47+
func (e *Entry) GetSubType() string {
48+
return e.SubType
49+
}
50+
51+
var entryFactories = map[string]func() EntryData{
52+
"Credential/Default": func() EntryData { return &EntryCredentialDefaultData{} },
53+
"Credential/AccessCode": func() EntryData { return &EntryCredentialAccessCodeData{} },
54+
}
3355

34-
tags[i] = unquotedTag
56+
func (e *Entry) UnmarshalJSON(data []byte) error {
57+
type alias Entry
58+
raw := &struct {
59+
Data json.RawMessage `json:"data"`
60+
*alias
61+
}{
62+
alias: (*alias)(e),
3563
}
3664

37-
return tags
65+
if err := json.Unmarshal(data, &raw); err != nil {
66+
return err
67+
}
68+
69+
key := fmt.Sprintf("%s/%s", raw.Type, raw.SubType)
70+
factory, ok := entryFactories[key]
71+
if !ok {
72+
return fmt.Errorf("unsupported entry type/subtype: %s", key)
73+
}
74+
75+
dataStruct := factory()
76+
if err := json.Unmarshal(raw.Data, dataStruct); err != nil {
77+
return fmt.Errorf("failed to unmarshal entry data: %w", err)
78+
}
79+
80+
e.Data = dataStruct
81+
82+
return nil
3883
}
3984

40-
func sliceToKeywords(kw []string) string {
41-
keywords := []string(kw)
42-
for i, v := range keywords {
43-
if strings.Contains(v, " ") {
44-
kw[i] = "\"" + v + "\""
45-
}
85+
func (e Entry) MarshalJSON() ([]byte, error) {
86+
type alias Entry
87+
88+
dataBytes, err := json.Marshal(e.Data)
89+
if err != nil {
90+
return nil, err
4691
}
4792

48-
kString := strings.Join(keywords, " ")
93+
return json.Marshal(&struct {
94+
Data json.RawMessage `json:"data"`
95+
*alias
96+
}{
97+
Data: dataBytes,
98+
alias: (*alias)(&e),
99+
})
100+
}
101+
102+
func entryPublicEndpointReplacer(vaultId string, entryId string) string {
103+
replacer := strings.NewReplacer("{vaultId}", vaultId, "{id}", entryId)
104+
return replacer.Replace(entryPublicEndpoint)
105+
}
49106

50-
return kString
107+
func entryPublicBaseEnpointReplacer(vaultId string) string {
108+
replacer := strings.NewReplacer("{vaultId}", vaultId)
109+
return replacer.Replace(entryBasePublicEndpoint)
51110
}
File renamed without changes.

entry_certificate_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ var (
1616
testCertificateEntry EntryCertificate = EntryCertificate{
1717
VaultId: testVaultId,
1818
Name: "TestK8sCertificate",
19-
Password: testEntryPassword,
19+
Password: "TestK8sCertificatePassword",
2020
Tags: []string{"test", "k8s"},
2121
CertificateIdentifier: "test",
2222
}

0 commit comments

Comments
 (0)