Skip to content

Commit

Permalink
feat: Added certificate entries
Browse files Browse the repository at this point in the history
  • Loading branch information
dion-gionet committed Jun 3, 2024
1 parent c9ea889 commit fba3532
Show file tree
Hide file tree
Showing 10 changed files with 645 additions and 38 deletions.
11 changes: 10 additions & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ jobs:
content: ${{ secrets.DEVOLUTIONS_HQ_ISSUING_CA_PEM }}
path: ${{ runner.temp }}/certificate.pem

- name: Download test certificate
uses: ./.github/workflows/create-file-from-secret
with:
content: ${{ secrets.TEST_CERTIFICATE }}
path: ${{ runner.temp }}/test.p12
format: base64

- name: Update CA store
run: |
sudo cp ${{ runner.temp }}/certificate.pem /usr/local/share/ca-certificates/certificate.crt
Expand All @@ -66,7 +73,9 @@ jobs:
TEST_USER: ${{ secrets.TEST_USER }}
TEST_PASSWORD: ${{ secrets.TEST_PASSWORD }}
TEST_INSTANCE: ${{ secrets.TEST_INSTANCE }}
TEST_ENTRY_ID: ${{ secrets.TEST_ENTRY_ID }}
TEST_USER_ENTRY_ID: ${{ secrets.TEST_USER_ENTRY_ID }}
TEST_CERTIFICATE_ENTRY_ID: ${{ secrets.TEST_CERTIFICATE_ENTRY_ID }}
TEST_CERTIFICATE_FILE_PATH: '${{ runner.temp }}/test.p12'
TEST_VAULT_ID: ${{ secrets.TEST_VAULT_ID }}
with:
github_token: ${{ secrets.DEVOLUTIONSBOT_TOKEN }}
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.6.0
0.7.0
89 changes: 89 additions & 0 deletions attachments.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package dvls

import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"net/url"
)

type EntryAttachment struct {
ID string `json:"id,omitempty"`
IDString string `json:"idString"`
EntryID string `json:"connectionID"`
EntryIDString string `json:"connectionIDString"`
Description string `json:"description"`
FileName string `json:"filename"`
IsPrivate bool `json:"isPrivate"`
Size int `json:"size"`
Title string `json:"title"`
}

// UnmarshalJSON implements the json.Unmarshaler interface.
func (e *EntryAttachment) UnmarshalJSON(d []byte) error {
type rawEntryAttachment EntryAttachment
raw := struct {
Data rawEntryAttachment `json:"data"`
}{}

err := json.Unmarshal(d, &raw)
if err != nil {
return err
}

*e = EntryAttachment(raw.Data)

return nil
}

const attachmentEndpoint = "/api/attachment"

func (c *Client) newAttachmentRequest(attachment EntryAttachment) (string, error) {
reqUrl, err := url.JoinPath(c.baseUri, attachmentEndpoint, "save?=&private=false&useSensitiveMode=true")
if err != nil {
return "", fmt.Errorf("failed to build attachment url. error: %w", err)
}

reqUrl, err = url.QueryUnescape(reqUrl)
if err != nil {
return "", fmt.Errorf("failed to unescape query url. error: %w", err)
}

entryJson, err := json.Marshal(attachment)
if err != nil {
return "", fmt.Errorf("failed to marshal body. error: %w", err)
}

resp, err := c.Request(reqUrl, http.MethodPost, bytes.NewBuffer(entryJson))
if err != nil {
return "", fmt.Errorf("error while submitting entry attachment request. error: %w", err)
} else if err = resp.CheckRespSaveResult(); err != nil {
return "", err
}

err = json.Unmarshal(resp.Response, &attachment)
if err != nil {
return "", fmt.Errorf("failed to unmarshal response body. error: %w", err)
}

return attachment.ID, nil
}

func (c *Client) uploadAttachment(fileBytes []byte, attachmentId string) error {
reqUrl, err := url.JoinPath(c.baseUri, attachmentEndpoint, attachmentId, "document")
if err != nil {
return fmt.Errorf("failed to build attachment url. error: %w", err)
}

contentType := http.DetectContentType(fileBytes)

resp, err := c.Request(reqUrl, http.MethodPost, bytes.NewBuffer(fileBytes), RequestOptions{ContentType: contentType})
if err != nil {
return fmt.Errorf("error while uploading entry attachment. error: %w", err)
} else if err = resp.CheckRespSaveResult(); err != nil {
return err
}

return nil
}
1 change: 1 addition & 0 deletions authentication.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ func NewClient(username string, password string, baseUri string) (Client, error)

client.Entries = &Entries{
UserCredential: (*EntryUserCredentialService)(&client.common),
Certificate: (*EntryCertificateService)(&client.common),
}
client.Vaults = (*Vaults)(&client.common)

Expand Down
11 changes: 8 additions & 3 deletions dvls.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type RequestError struct {

type RequestOptions struct {
ContentType string
RawBody bool
}

func (e RequestError) Error() string {
Expand Down Expand Up @@ -50,9 +51,11 @@ func (c *Client) Request(url string, reqMethod string, reqBody io.Reader, option

func (c *Client) rawRequest(url string, reqMethod string, reqBody io.Reader, options ...RequestOptions) (Response, error) {
contentType := "application/json"
var rawBody bool

if len(options) > 0 {
contentType = options[0].ContentType
rawBody = options[0].RawBody
}

req, err := http.NewRequest(reqMethod, url, reqBody)
Expand All @@ -77,9 +80,11 @@ func (c *Client) rawRequest(url string, reqMethod string, reqBody io.Reader, opt
}
defer resp.Body.Close()

err = json.Unmarshal(response.Response, &response)
if err != nil {
return response, &RequestError{Err: fmt.Errorf("failed to unmarshal response body. error: %w", err), Url: url}
if !rawBody {
err = json.Unmarshal(response.Response, &response)
if err != nil {
return response, &RequestError{Err: fmt.Errorf("failed to unmarshal response body. error: %w", err), Url: url}
}
}

return response, nil
Expand Down
2 changes: 0 additions & 2 deletions dvls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@ import (

var (
testClient Client
testEntryId string
testVaultId string
)

func TestMain(m *testing.M) {
testEntryId = os.Getenv("TEST_ENTRY_ID")
testVaultId = os.Getenv("TEST_VAULT_ID")

err := setupTestClient()
Expand Down
3 changes: 2 additions & 1 deletion dvlstypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,8 @@ const (
type ServerConnectionSubType string

const (
ServerConnectionSubTypeDefault ServerConnectionSubType = "Default"
ServerConnectionSubTypeDefault ServerConnectionSubType = "Default"
ServerConnectionSubTypeCertificate ServerConnectionSubType = "Certificate"
)

type VaultVisibility int
Expand Down
Loading

0 comments on commit fba3532

Please sign in to comment.