155 lines
4.0 KiB
Go
155 lines
4.0 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/base64"
|
|
"strings"
|
|
|
|
"certmanager/pkg/client"
|
|
"certmanager/pkg/cmctl"
|
|
)
|
|
|
|
func (util *Util) CreateServicePair(ctx context.Context) (*cmctl.CreateServicePairResult, error) {
|
|
var err error
|
|
res := &cmctl.CreateServicePairResult{}
|
|
cli, err := client.NewClient(&util.access)
|
|
if err != nil {
|
|
return res, err
|
|
}
|
|
inetAddresses := make([]string, 0)
|
|
hostnames := make([]string, 0)
|
|
if util.ipAdressesList != "" {
|
|
inetAddresses = strings.Split(util.ipAdressesList, ",")
|
|
}
|
|
if util.hostnameList != "" {
|
|
hostnames = strings.Split(util.hostnameList, ",")
|
|
}
|
|
params := &cmctl.CreateServicePairParams{
|
|
IssuerName: util.issuerName,
|
|
IssuerID: util.issuerID,
|
|
ServiceOrganizationName: util.serviceOrganizationName,
|
|
ServiceOrganizationalUnitName: util.serviceOrganizationalUnitName,
|
|
ServiceCommonName: util.serviceCommonName,
|
|
InetAddresses: inetAddresses,
|
|
Hostnames: hostnames,
|
|
}
|
|
res, err = cli.CreateServicePair(ctx, params)
|
|
if err != nil {
|
|
return res, err
|
|
}
|
|
certPEM, err := base64.StdEncoding.DecodeString(res.Certificate)
|
|
if err != nil {
|
|
return res, err
|
|
}
|
|
res.Certificate = string(certPEM)
|
|
keyPEM, err := base64.StdEncoding.DecodeString(res.Key)
|
|
if err != nil {
|
|
return res, err
|
|
}
|
|
res.Key = string(keyPEM)
|
|
caPEM, err := base64.StdEncoding.DecodeString(res.IssuerCertificate)
|
|
if err != nil {
|
|
return res, err
|
|
}
|
|
res.IssuerCertificate = string(caPEM)
|
|
for i, cert := range res.IssuerCertificates {
|
|
caPEM, err := base64.StdEncoding.DecodeString(cert)
|
|
if err != nil {
|
|
return res, err
|
|
}
|
|
res.IssuerCertificates[i] = string(caPEM)
|
|
}
|
|
return res, err
|
|
}
|
|
|
|
func (util *Util) RevokeServicePair(ctx context.Context) (*cmctl.RevokeServicePairResult, error) {
|
|
var err error
|
|
res := &cmctl.RevokeServicePairResult{}
|
|
cli, err := client.NewClient(&util.access)
|
|
if err != nil {
|
|
return res, err
|
|
}
|
|
params := &cmctl.RevokeServicePairParams{
|
|
ServiceName: util.serviceName,
|
|
ServiceID: util.serviceID,
|
|
}
|
|
res, err = cli.RevokeServicePair(ctx, params)
|
|
if err != nil {
|
|
return res, err
|
|
}
|
|
return res, err
|
|
}
|
|
|
|
func (util *Util) UnrevokeServicePair(ctx context.Context) (*cmctl.UnrevokeServicePairResult, error) {
|
|
var err error
|
|
res := &cmctl.UnrevokeServicePairResult{}
|
|
cli, err := client.NewClient(&util.access)
|
|
if err != nil {
|
|
return res, err
|
|
}
|
|
params := &cmctl.UnrevokeServicePairParams{
|
|
ServiceName: util.serviceName,
|
|
ServiceID: util.serviceID,
|
|
}
|
|
res, err = cli.UnrevokeServicePair(ctx, params)
|
|
if err != nil {
|
|
return res, err
|
|
}
|
|
return res, err
|
|
}
|
|
|
|
func (util *Util) ListServicePairs(ctx context.Context) (*cmctl.ListServicePairsResult, error) {
|
|
var err error
|
|
res := &cmctl.ListServicePairsResult{}
|
|
cli, err := client.NewClient(&util.access)
|
|
if err != nil {
|
|
return res, err
|
|
}
|
|
params := &cmctl.ListServicePairsParams{}
|
|
res, err = cli.ListServicePairs(ctx, params)
|
|
if err != nil {
|
|
return res, err
|
|
}
|
|
return res, err
|
|
}
|
|
|
|
func (util *Util) GetServicePair(ctx context.Context) (*cmctl.GetServicePairResult, error) {
|
|
var err error
|
|
res := &cmctl.GetServicePairResult{}
|
|
cli, err := client.NewClient(&util.access)
|
|
if err != nil {
|
|
return res, err
|
|
}
|
|
params := &cmctl.GetServicePairParams{
|
|
ServiceID: util.serviceID,
|
|
ServiceName: util.serviceName,
|
|
}
|
|
res, err = cli.GetServicePair(ctx, params)
|
|
if err != nil {
|
|
return res, err
|
|
}
|
|
certPEM, err := base64.StdEncoding.DecodeString(res.Certificate)
|
|
if err != nil {
|
|
return res, err
|
|
}
|
|
res.Certificate = string(certPEM)
|
|
keyPEM, err := base64.StdEncoding.DecodeString(res.Key)
|
|
if err != nil {
|
|
return res, err
|
|
}
|
|
res.Key = string(keyPEM)
|
|
caPEM, err := base64.StdEncoding.DecodeString(res.IssuerCertificate)
|
|
if err != nil {
|
|
return res, err
|
|
}
|
|
res.IssuerCertificate = string(caPEM)
|
|
for i, cert := range res.IssuerCertificates {
|
|
caPEM, err := base64.StdEncoding.DecodeString(cert)
|
|
if err != nil {
|
|
return res, err
|
|
}
|
|
res.IssuerCertificates[i] = string(caPEM)
|
|
}
|
|
return res, err
|
|
}
|