|
| 1 | +package certutils |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "strings" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/aws/aws-sdk-go/aws" |
| 10 | + "github.com/aws/aws-sdk-go/aws/session" |
| 11 | + "github.com/aws/aws-sdk-go/service/acm" |
| 12 | + "github.com/olekukonko/tablewriter" |
| 13 | +) |
| 14 | + |
| 15 | +type ACM struct { |
| 16 | + client *acm.ACM |
| 17 | +} |
| 18 | + |
| 19 | +type ACMDescription struct { |
| 20 | + arn string |
| 21 | + nameTag string |
| 22 | + status string |
| 23 | + inUseBy []string |
| 24 | + notAfter time.Time |
| 25 | + domainName string |
| 26 | + subjectAlternativeNames []string |
| 27 | +} |
| 28 | + |
| 29 | +func NewACM(sess *session.Session) *ACM { |
| 30 | + return &ACM{ |
| 31 | + client: acm.New(sess), |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +func createACMImportCertificateInput(cert, chain, pkey []byte) *acm.ImportCertificateInput { |
| 36 | + return &acm.ImportCertificateInput{ |
| 37 | + Certificate: cert, |
| 38 | + CertificateChain: chain, |
| 39 | + PrivateKey: pkey, |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +func (a *ACM) Import(cert, chain, pkey []byte) (string, string, error) { |
| 44 | + out, err := a.client.ImportCertificate(createACMImportCertificateInput(cert, chain, pkey)) |
| 45 | + |
| 46 | + return *out.CertificateArn, fmt.Sprintf("Imported %s", *out.CertificateArn), err |
| 47 | +} |
| 48 | + |
| 49 | +func createACMListCertificatesInput(statuses []string, maxItems int64, nextToken string) *acm.ListCertificatesInput { |
| 50 | + linput := &acm.ListCertificatesInput{} |
| 51 | + |
| 52 | + if len(statuses) > 0 { |
| 53 | + linput.SetCertificateStatuses(aws.StringSlice(statuses)) |
| 54 | + } |
| 55 | + |
| 56 | + if maxItems > 0 { |
| 57 | + linput.SetMaxItems(maxItems) |
| 58 | + } |
| 59 | + |
| 60 | + if nextToken != "" { |
| 61 | + linput.SetNextToken(nextToken) |
| 62 | + } |
| 63 | + |
| 64 | + return linput |
| 65 | +} |
| 66 | + |
| 67 | +func (a *ACM) listTags(arn string) (*acm.ListTagsForCertificateOutput, error) { |
| 68 | + input := &acm.ListTagsForCertificateInput{} |
| 69 | + input.SetCertificateArn(arn) |
| 70 | + |
| 71 | + return a.client.ListTagsForCertificate(input) |
| 72 | +} |
| 73 | + |
| 74 | +func (a *ACM) getNameTag(arn string) (string, error) { |
| 75 | + out, err := a.listTags(arn) |
| 76 | + if err != nil { |
| 77 | + return "", err |
| 78 | + } |
| 79 | + |
| 80 | + for _, tag := range out.Tags { |
| 81 | + if strings.ToLower(aws.StringValue(tag.Key)) == "name" { |
| 82 | + return *tag.Value, nil |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + return "", fmt.Errorf("Name tag not found") |
| 87 | +} |
| 88 | + |
| 89 | +func (a *ACM) List(statuses string, maxItems int64, nextToken string) ([]ACMDescription, error) { |
| 90 | + cout, err := a.client.ListCertificates(createACMListCertificatesInput(SplitStatuses(statuses), maxItems, nextToken)) |
| 91 | + |
| 92 | + descs := make([]ACMDescription, 0, len(cout.CertificateSummaryList)) |
| 93 | + for _, summary := range cout.CertificateSummaryList { |
| 94 | + dcout, err := a.client.DescribeCertificate(&acm.DescribeCertificateInput{ |
| 95 | + CertificateArn: summary.CertificateArn, |
| 96 | + }) |
| 97 | + if err != nil { |
| 98 | + return []ACMDescription{}, err |
| 99 | + } |
| 100 | + |
| 101 | + cert := dcout.Certificate |
| 102 | + nameTag, _ := a.getNameTag(*cert.CertificateArn) |
| 103 | + |
| 104 | + desc := ACMDescription{ |
| 105 | + arn: *cert.CertificateArn, |
| 106 | + nameTag: nameTag, |
| 107 | + status: *cert.Status, |
| 108 | + inUseBy: aws.StringValueSlice(cert.InUseBy), |
| 109 | + notAfter: aws.TimeValue(cert.NotAfter), |
| 110 | + domainName: *cert.DomainName, |
| 111 | + subjectAlternativeNames: aws.StringValueSlice(cert.SubjectAlternativeNames), |
| 112 | + } |
| 113 | + |
| 114 | + descs = append(descs, desc) |
| 115 | + } |
| 116 | + |
| 117 | + return descs, err |
| 118 | +} |
| 119 | + |
| 120 | +func (a *ACM) ListArns(statuses string, maxItems int64, nextToken string) ([]string, error) { |
| 121 | + descs, err := a.List(statuses, maxItems, nextToken) |
| 122 | + |
| 123 | + arns := make([]string, 0, len(descs)) |
| 124 | + for _, desc := range descs { |
| 125 | + arns = append(arns, desc.arn) |
| 126 | + } |
| 127 | + |
| 128 | + return arns, err |
| 129 | +} |
| 130 | + |
| 131 | +func toACMTags(tags []Tag) []*acm.Tag { |
| 132 | + if len(tags) <= 0 { |
| 133 | + return []*acm.Tag{} |
| 134 | + } |
| 135 | + |
| 136 | + acmTags := make([]*acm.Tag, 0, len(tags)) |
| 137 | + |
| 138 | + for _, t := range tags { |
| 139 | + acmTag := &acm.Tag{ |
| 140 | + Key: aws.String(t.Key), |
| 141 | + Value: aws.String(t.Value), |
| 142 | + } |
| 143 | + |
| 144 | + acmTags = append(acmTags, acmTag) |
| 145 | + } |
| 146 | + |
| 147 | + return acmTags |
| 148 | +} |
| 149 | + |
| 150 | +func createACMAddTagsToCertificateInput(arn string, tags []Tag) *acm.AddTagsToCertificateInput { |
| 151 | + tinput := &acm.AddTagsToCertificateInput{} |
| 152 | + tinput.SetCertificateArn(arn) |
| 153 | + tinput.SetTags(toACMTags(tags)) |
| 154 | + |
| 155 | + return tinput |
| 156 | +} |
| 157 | + |
| 158 | +func (a *ACM) AddTags(arn string, tags []Tag) error { |
| 159 | + _, err := a.client.AddTagsToCertificate(createACMAddTagsToCertificateInput(arn, tags)) |
| 160 | + |
| 161 | + return err |
| 162 | +} |
| 163 | + |
| 164 | +func createACMDeleteCertificateInput(arn string) *acm.DeleteCertificateInput { |
| 165 | + dinput := &acm.DeleteCertificateInput{} |
| 166 | + |
| 167 | + dinput.SetCertificateArn(arn) |
| 168 | + |
| 169 | + return dinput |
| 170 | +} |
| 171 | + |
| 172 | +func (a *ACM) Delete(arn string) (string, error) { |
| 173 | + _, err := a.client.DeleteCertificate(createACMDeleteCertificateInput(arn)) |
| 174 | + |
| 175 | + return fmt.Sprintf("Deleted %s", arn), err |
| 176 | +} |
| 177 | + |
| 178 | +func (a *ACM) ReadableList(descs []ACMDescription) { |
| 179 | + table := tablewriter.NewWriter(os.Stdout) |
| 180 | + |
| 181 | + table.SetHeader([]string{"Name tag", "Domain Name", "Additional Name", "In Use?", "Not After", "Certificate Arn"}) |
| 182 | + table.SetAutoMergeCells(true) |
| 183 | + table.SetRowLine(true) |
| 184 | + |
| 185 | + for _, desc := range descs { |
| 186 | + inUse := "No" |
| 187 | + if len(desc.inUseBy) > 0 { |
| 188 | + inUse = "Yes" |
| 189 | + } |
| 190 | + for _, name := range desc.subjectAlternativeNames { |
| 191 | + if name == desc.domainName { |
| 192 | + continue |
| 193 | + } |
| 194 | + table.Append([]string{desc.nameTag, desc.domainName, name, inUse, desc.notAfter.String(), desc.arn}) |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | + table.Render() |
| 199 | +} |
0 commit comments