-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Zhiwei Yin <[email protected]>
- Loading branch information
1 parent
00d2026
commit 95c4484
Showing
7 changed files
with
194 additions
and
21 deletions.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package common | ||
|
||
import ( | ||
"bytes" | ||
"crypto/x509" | ||
"encoding/pem" | ||
certutil "k8s.io/client-go/util/cert" | ||
"reflect" | ||
) | ||
|
||
func MergeCertificateData(caBundles ...[]byte) ([]byte, error) { | ||
var all []*x509.Certificate | ||
for _, caBundle := range caBundles { | ||
if len(caBundle) == 0 { | ||
continue | ||
} | ||
|
||
certs, err := certutil.ParseCertsPEM(caBundle) | ||
if err != nil { | ||
return []byte{}, err | ||
} | ||
all = append(all, certs...) | ||
} | ||
|
||
// remove duplicated cert | ||
var merged []*x509.Certificate | ||
for i := range all { | ||
found := false | ||
for j := range merged { | ||
if reflect.DeepEqual(all[i].Raw, merged[j].Raw) { | ||
found = true | ||
break | ||
} | ||
} | ||
if !found { | ||
merged = append(merged, all[i]) | ||
} | ||
} | ||
|
||
// encode the merged certificates | ||
b := bytes.Buffer{} | ||
for _, cert := range merged { | ||
if err := pem.Encode(&b, &pem.Block{Type: "CERTIFICATE", Bytes: cert.Raw}); err != nil { | ||
return []byte{}, err | ||
} | ||
} | ||
return b.Bytes(), 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
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