|
1 | 1 | package dvls |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "strconv" |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
5 | 6 | "strings" |
6 | 7 | ) |
7 | 8 |
|
8 | 9 | const ( |
9 | 10 | entryEndpoint string = "/api/connections/partial" |
10 | 11 | entryConnectionsEndpoint string = "/api/connections" |
| 12 | + entryBasePublicEndpoint string = "/api/v1/vault/{vaultId}/entry" |
| 13 | + entryPublicEndpoint string = "/api/v1/vault/{vaultId}/entry/{id}" |
11 | 14 | ) |
12 | 15 |
|
13 | 16 | 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 | +} |
33 | 55 |
|
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), |
35 | 63 | } |
36 | 64 |
|
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 |
38 | 83 | } |
39 | 84 |
|
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 |
46 | 91 | } |
47 | 92 |
|
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 entryPublicEnpointReplacer(vaultId string, entryId string) string { |
| 103 | + replacer := strings.NewReplacer("{vaultId}", vaultId, "{id}", entryId) |
| 104 | + return replacer.Replace(entryPublicEndpoint) |
| 105 | +} |
49 | 106 |
|
50 | | - return kString |
| 107 | +func entryPublicBaseEnpointReplacer(vaultId string) string { |
| 108 | + replacer := strings.NewReplacer("{vaultId}", vaultId) |
| 109 | + return replacer.Replace(entryBasePublicEndpoint) |
51 | 110 | } |
0 commit comments