Skip to content

Commit 939429c

Browse files
authored
Merge pull request #57 from gruntwork-io/yori-fix-bug-in-multinamed-json
Fix bug where tls subject info did not allow the second name for the keys
2 parents 0a591c2 + 3f141ea commit 939429c

File tree

3 files changed

+61
-6
lines changed

3 files changed

+61
-6
lines changed

.circleci/config.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ defaults: &defaults
55
environment:
66
GRUNTWORK_INSTALLER_VERSION: v0.0.21
77
TERRATEST_LOG_PARSER_VERSION: v0.13.13
8-
MODULE_CI_VERSION: v0.13.12
8+
MODULE_CI_VERSION: v0.14.1
99
TERRAFORM_VERSION: NONE
1010
TERRAGRUNT_VERSION: NONE
1111
PACKER_VERSION: NONE
1212
GOLANG_VERSION: 1.11.2
1313
KUBECONFIG: /home/circleci/.kube/config
14-
HELM_VERSION: v2.14.0
14+
HELM_VERSION: v2.14.0
1515

1616

1717
install_helm_client: &install_helm_client

cmd/common.go

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,25 @@ var (
101101

102102
type TLSSubjectInfo struct {
103103
CommonName string `json:"common_name"`
104-
Org string `json:"org" json:"organization"`
105-
OrgUnit string `json:"org_unit" json:"organizational_unit"`
106-
City string `json:"city" json:"locality"`
107-
State string `json:"state" json:"province"`
108104
Country string `json:"country"`
105+
106+
// These fields have multiple names that they can be encoded as, so we use a different field to handle those.
107+
Org string
108+
OrgUnit string
109+
City string
110+
State string
111+
112+
// These fields are used to handle multiple json encodings for the actual field. For example, Org can be encoded in
113+
// json as "org" or "organization". Which one is preferred in the case where both encodings are provided is
114+
// arbitrary (undefined behavior).
115+
OrgEncodedAsOrg *string `json:"org,omitempty"`
116+
OrgEncodedAsOrganization *string `json:"organization,omitempty"`
117+
OrgUnitEncodedAsOrgUnit *string `json:"org_unit,omitempty"`
118+
OrgUnitEncodedAsOrganizationalUnit *string `json:"organizational_unit,omitempty"`
119+
CityEncodedAsCity *string `json:"city,omitempty"`
120+
CityEncodedAsLocality *string `json:"locality,omitempty"`
121+
StateEncodedAsState *string `json:"state,omitempty" json:"province"`
122+
StateEncodedAsProvince *string `json:"province,omitempty"`
109123
}
110124

111125
type TLSFlags struct {
@@ -128,9 +142,27 @@ func parseOrCreateTLSSubjectInfo(jsonString string) (TLSSubjectInfo, error) {
128142
return subjectInfo, errors.WithStackTrace(err)
129143
}
130144
}
145+
146+
// Reconcile multiple encoding name fields
147+
subjectInfo.Org = getStringFromEncodingOrEmpty(subjectInfo.OrgEncodedAsOrg, subjectInfo.OrgEncodedAsOrganization)
148+
subjectInfo.OrgUnit = getStringFromEncodingOrEmpty(subjectInfo.OrgUnitEncodedAsOrgUnit, subjectInfo.OrgUnitEncodedAsOrganizationalUnit)
149+
subjectInfo.City = getStringFromEncodingOrEmpty(subjectInfo.CityEncodedAsCity, subjectInfo.CityEncodedAsLocality)
150+
subjectInfo.State = getStringFromEncodingOrEmpty(subjectInfo.StateEncodedAsState, subjectInfo.StateEncodedAsProvince)
151+
131152
return subjectInfo, nil
132153
}
133154

155+
// setStringFromEncoding will return the first non-empty string from the list of strings passed in, or empty string if
156+
// they are all unset.
157+
func getStringFromEncodingOrEmpty(encodings ...*string) string {
158+
for _, encoding := range encodings {
159+
if encoding != nil && *encoding != "" {
160+
return *encoding
161+
}
162+
}
163+
return ""
164+
}
165+
134166
// parseTLSFlagsToPkixName takes the CLI args related to setting up the Distinguished Name identifier of the TLS
135167
// certificate and converts them to the pkix.Name struct.
136168
func parseTLSFlagsToPkixName(cliContext *cli.Context, tlsFlags TLSFlags) (pkix.Name, error) {

cmd/common_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package main
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestParseTLSSubjectInfoJsonOrgOrgUnit(t *testing.T) {
10+
t.Parallel()
11+
subjectInfo, err := parseOrCreateTLSSubjectInfo(`{"org": "Gruntwork", "org_unit": "Eng"}`)
12+
assert.NoError(t, err)
13+
assert.Equal(t, subjectInfo.Org, "Gruntwork")
14+
assert.Equal(t, subjectInfo.OrgUnit, "Eng")
15+
}
16+
17+
func TestParseTLSSubjectInfoJsonOrganizationOrganizationalUnit(t *testing.T) {
18+
t.Parallel()
19+
subjectInfo, err := parseOrCreateTLSSubjectInfo(`{"organization": "Gruntwork", "organizational_unit": "Eng"}`)
20+
assert.NoError(t, err)
21+
assert.Equal(t, subjectInfo.Org, "Gruntwork")
22+
assert.Equal(t, subjectInfo.OrgUnit, "Eng")
23+
}

0 commit comments

Comments
 (0)