Skip to content

Commit db8511c

Browse files
committed
bug fix
1 parent 4994511 commit db8511c

1 file changed

Lines changed: 51 additions & 38 deletions

File tree

  • engine/plugins/service_discovery/http_probes

engine/plugins/service_discovery/http_probes/plugin.go

Lines changed: 51 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -122,45 +122,8 @@ func (hp *httpProbing) store(e *et.Event, resp *http.Response, entity *dbt.Entit
122122
serv.OutputLen = int(resp.Length)
123123
serv.Attributes = resp.Header
124124

125-
var firstAsset *dbt.Entity
126-
var firstCert *x509.Certificate
127-
var findings []*support.Finding
128-
if count := len(resp.TLS.PeerCertificates); resp.TLS != nil && resp.TLS.HandshakeComplete && count > 0 {
129-
dur := time.Duration(count*3) * time.Second
130-
ctx, cancel := context.WithTimeout(e.Session.Ctx(), dur)
131-
defer cancel()
132-
133-
var prev *dbt.Entity
134-
// traverse the certificate chain
135-
for _, cert := range resp.TLS.PeerCertificates {
136-
c := support.X509ToOAMTLSCertificate(cert)
137-
if c == nil {
138-
break
139-
}
140-
141-
a, err := e.Session.DB().CreateAsset(ctx, c)
142-
if err != nil {
143-
break
144-
}
145-
146-
if prev == nil {
147-
firstAsset = a
148-
firstCert = cert
149-
} else if tls, valid := prev.Asset.(*oamcert.TLSCertificate); valid {
150-
findings = append(findings, &support.Finding{
151-
From: prev,
152-
FromName: tls.SerialNumber,
153-
To: a,
154-
ToName: c.SerialNumber,
155-
ToMeta: cert,
156-
Rel: &general.SimpleRelation{Name: "issuing_certificate"},
157-
})
158-
}
159-
prev = a
160-
}
161-
}
162-
163125
var c *oamcert.TLSCertificate
126+
firstAsset, firstCert, findings := hp.createCertificates(e.Session, resp)
164127
if firstAsset != nil {
165128
var valid bool
166129
c, valid = firstAsset.Asset.(*oamcert.TLSCertificate)
@@ -206,3 +169,53 @@ func (hp *httpProbing) store(e *et.Event, resp *http.Response, entity *dbt.Entit
206169
}
207170
return findings
208171
}
172+
173+
func (hp *httpProbing) createCertificates(sess et.Session, resp *http.Response) (*dbt.Entity, *x509.Certificate, []*support.Finding) {
174+
var findings []*support.Finding
175+
176+
if resp.TLS == nil || !resp.TLS.HandshakeComplete {
177+
return nil, nil, findings
178+
}
179+
180+
count := len(resp.TLS.PeerCertificates)
181+
if count == 0 {
182+
return nil, nil, findings
183+
}
184+
185+
dur := time.Duration(count*3) * time.Second
186+
ctx, cancel := context.WithTimeout(sess.Ctx(), dur)
187+
defer cancel()
188+
189+
var prev *dbt.Entity
190+
var firstAsset *dbt.Entity
191+
var firstCert *x509.Certificate
192+
// traverse the certificate chain
193+
for _, cert := range resp.TLS.PeerCertificates {
194+
c := support.X509ToOAMTLSCertificate(cert)
195+
if c == nil {
196+
break
197+
}
198+
199+
a, err := sess.DB().CreateAsset(ctx, c)
200+
if err != nil {
201+
break
202+
}
203+
204+
if prev == nil {
205+
firstAsset = a
206+
firstCert = cert
207+
} else if tls, valid := prev.Asset.(*oamcert.TLSCertificate); valid {
208+
findings = append(findings, &support.Finding{
209+
From: prev,
210+
FromName: tls.SerialNumber,
211+
To: a,
212+
ToName: c.SerialNumber,
213+
ToMeta: cert,
214+
Rel: &general.SimpleRelation{Name: "issuing_certificate"},
215+
})
216+
}
217+
prev = a
218+
}
219+
220+
return firstAsset, firstCert, findings
221+
}

0 commit comments

Comments
 (0)