-
Notifications
You must be signed in to change notification settings - Fork 63
/
search.go
145 lines (125 loc) · 3.93 KB
/
search.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
* Copyright 2018 Venafi, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package tpp
import (
"crypto/sha1"
"encoding/json"
"encoding/pem"
"fmt"
"github.com/Venafi/vcert/v4/pkg/certificate"
"net/http"
"strings"
)
type SearchRequest []string
type ConfigReadDNRequest struct {
ObjectDN string `json:",omitempty"`
AttributeName string `json:",omitempty"`
}
type ConfigReadDNResponse struct {
Result int `json:",omitempty"`
Values []string `json:",omitempty"`
}
type CertificateDetailsResponse struct {
CustomFields []struct {
Name string
Value []string
}
Consumers []string
}
type CertificateSearchResponse struct {
Certificates []Certificate `json:"Certificates"`
Count int `json:"TotalCount"`
}
type Certificate struct {
//Id string `json:"DN"`
//ManagedCertificateId string `json:"DN"`
CertificateRequestId string `json:"DN"`
CertificateRequestGuid string `json:"Guid"`
/*...and some more fields... */
}
func (c *Connector) searchCertificatesByFingerprint(fp string) (*certificate.CertSearchResponse, error) {
fp = strings.Replace(fp, ":", "", -1)
fp = strings.Replace(fp, ".", "", -1)
fp = strings.ToUpper(fp)
var req certificate.SearchRequest
req = append(req, fmt.Sprintf("Thumbprint=%s", fp))
return c.SearchCertificates(&req)
}
func (c *Connector) configReadDN(req ConfigReadDNRequest) (resp ConfigReadDNResponse, err error) {
statusCode, status, body, err := c.request("POST", urlResourceConfigReadDn, req)
if err != nil {
return resp, err
}
if statusCode == http.StatusOK {
err = json.Unmarshal(body, &resp)
if err != nil {
return resp, err
}
} else {
return resp, fmt.Errorf("unexpected status code on %s. Status: %s", urlResourceConfigReadDn, status)
}
return resp, nil
}
func (c *Connector) searchCertificateDetails(guid string) (*CertificateDetailsResponse, error) {
var err error
url := fmt.Sprintf("%s%s", urlResourceCertificateSearch, guid)
statusCode, _, body, err := c.request("GET", urlResource(url), nil)
if err != nil {
return nil, err
}
return parseCertificateDetailsResponse(statusCode, body)
}
func parseCertificateDetailsResponse(statusCode int, body []byte) (searchResult *CertificateDetailsResponse, err error) {
switch statusCode {
case http.StatusOK:
var searchResult = &CertificateDetailsResponse{}
err = json.Unmarshal(body, searchResult)
if err != nil {
return nil, fmt.Errorf("Failed to parse search results: %s, body: %s", err, body)
}
return searchResult, nil
default:
if body != nil {
return nil, NewResponseError(body)
} else {
return nil, fmt.Errorf("Unexpected status code on certificate search. Status: %d", statusCode)
}
}
}
func ParseCertificateSearchResponse(httpStatusCode int, body []byte) (searchResult *certificate.CertSearchResponse, err error) {
switch httpStatusCode {
case http.StatusOK:
var searchResult = &certificate.CertSearchResponse{}
err = json.Unmarshal(body, searchResult)
if err != nil {
return nil, fmt.Errorf("Failed to parse search results: %s, body: %s", err, body)
}
return searchResult, nil
default:
if body != nil {
return nil, NewResponseError(body)
} else {
return nil, fmt.Errorf("Unexpected status code on certificate search. Status: %d", httpStatusCode)
}
}
}
func calcThumbprint(cert string) string {
p, _ := pem.Decode([]byte(cert))
h := sha1.New()
h.Write(p.Bytes)
buf := h.Sum(nil)
return strings.ToUpper(fmt.Sprintf("%x", buf))
}