-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #34 from android-leha/certificates
Adding certificate API
- Loading branch information
Showing
5 changed files
with
166 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(¶ms) | ||
|
||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters