Skip to content

Commit

Permalink
Merge pull request #34 from android-leha/certificates
Browse files Browse the repository at this point in the history
Adding certificate API
  • Loading branch information
Nosmoht committed Jul 4, 2020
2 parents 21dae35 + 31f8d08 commit 7937bad
Show file tree
Hide file tree
Showing 5 changed files with 166 additions and 1 deletion.
131 changes: 131 additions & 0 deletions certificate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package client

import (
"encoding/json"
"fmt"
"github.com/google/go-querystring/query"
"net/http"
"net/url"
"strconv"
"strings"
)

const (
certificateAPIEndpoint = "service/rest/beta/security/ssl"
)

type Certificate struct {
Id string `json:"id"`
Fingerprint string `json:"fingerprint"`
SerialNumber string `json:"serialNumber"`
IssuerCommonName string `json:"issuerCommonName"`
IssuerOrganization string `json:"issuerOrganization"`
IssuerOrganizationUnit string `json:"issuerOrganizationalUnit"`
SubjectCommonName string `json:"subjectCommonName"`
SubjectOrganization string `json:"subjectOrganization"`
SubjectOrganizationUnit string `json:"subjectOrganizationalUnit"`
Pem string `json:"pem"`
IssuedOn int64 `json:"issuedOn"`
ExpiresOn int64 `json:"expiresOn"`
}

type CertificateRequest struct {
Host string `url:"host"`
Port int `url:"port"`
}

func NewCertificateRequest(proxyUrl string) (*CertificateRequest, error) {
data, err := url.Parse(proxyUrl)
if err != nil {
return nil, err
}
port := 443
if data.Port() != "" {
port, err = strconv.Atoi(data.Port())
if err != nil {
return nil, err
}
}
request := &CertificateRequest{data.Hostname(), port}
return request, nil
}

func jsonUnmarshalCertificate(data []byte) (*Certificate, error) {
var certificate = Certificate{}
if err := json.Unmarshal(data, &certificate); err != nil {
return nil, fmt.Errorf("could not unmarshal certificate: %v", err)
}
return &certificate, nil
}

func jsonUnmarshalCertificateList(data []byte) (*[]Certificate, error) {
var certificates []Certificate
if err := json.Unmarshal(data, &certificates); err != nil {
return nil, fmt.Errorf("could not unmarshal certificates: %v", err)
}
return &certificates, nil
}

func (c client) CertificateCreate(certificate *Certificate) error {
data := strings.NewReader(certificate.Pem)

body, resp, err := c.Post(fmt.Sprintf("%s/truststore", certificateAPIEndpoint), data)
if err != nil {
return err
}

if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated {
return fmt.Errorf("could not create certificate '%s': HTTP: %d, %s", certificate.Id, resp.StatusCode, string(body))
}
return nil
}

func (c client) CertificateDelete(id string) error {
body, resp, err := c.Delete(fmt.Sprintf("%s/truststore/%s", certificateAPIEndpoint, url.QueryEscape(id)))
if err != nil {
return err
}
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusNoContent {
return fmt.Errorf("could not delete certificate '%s': HTTP: %d, %s", id, resp.StatusCode, string(body))
}
return nil
}

func (c client) CertificateList() (*[]Certificate, error) {
body, resp, err := c.Get(fmt.Sprintf("%s/truststore", certificateAPIEndpoint), nil)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("could not get certificates: HTTP: %d, %s", resp.StatusCode, string(body))
}

certificates, err := jsonUnmarshalCertificateList(body)

if err != nil {
return nil, err
}

return certificates, nil
}

func (c client) CertificateGet(params *CertificateRequest) (*Certificate, error) {
values, _ := query.Values(&params)

body, resp, err := c.Get(fmt.Sprintf("%s?%s", certificateAPIEndpoint, values.Encode()), nil)

if err != nil {
return nil, err
}

if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("could not get certificate from '%s:%d': HTTP: %d, %s", params.Host, params.Port, resp.StatusCode, string(body))
}

certificate, err := jsonUnmarshalCertificate(body)
if err != nil {
return nil, err
}

return certificate, nil
}
25 changes: 25 additions & 0 deletions certificate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package client

import (
"fmt"
"github.com/stretchr/testify/assert"
"testing"
)

func TestCreateCertificate(t *testing.T) {
client := NewClient(getDefaultConfig())
request, err := NewCertificateRequest("https://www.google.com")
assert.Nil(t, err)
fmt.Printf("%#v\n", request)

certificate, err := client.CertificateGet(request)
assert.Nil(t, err)
assert.NotNil(t, certificate)
fmt.Printf("%#v\n", certificate)

err = client.CertificateCreate(certificate)
assert.Nil(t, err)

err = client.CertificateDelete(certificate.Id)
assert.Nil(t, err)
}
4 changes: 4 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ type Client interface {
BlobstoreDelete(string) error
BlobstoreRead(string) (*Blobstore, error)
BlobstoreUpdate(string, Blobstore) error
CertificateList() (*[]Certificate, error)
CertificateGet(*CertificateRequest) (*Certificate, error)
CertificateCreate(*Certificate) error
CertificateDelete(string) error
ContentSelectorCreate(ContentSelector) error
ContentSelectorDelete(string) error
ContentSelectorRead(string) (*ContentSelector, error)
Expand Down
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ module github.com/datadrivers/go-nexus-client

go 1.14

require github.com/stretchr/testify v1.4.0
require (
github.com/google/go-querystring v1.0.0
github.com/stretchr/testify v1.4.0
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/go-querystring v1.0.0 h1:Xkwi/a1rcvNg1PPYe5vI8GbeBY/jrVuDX5ASuANWTrk=
github.com/google/go-querystring v1.0.0/go.mod h1:odCYkC5MyYFN7vkCjXpyrEuKhc/BUO6wN/zVPAxq5ck=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
Expand Down

0 comments on commit 7937bad

Please sign in to comment.