Skip to content

Commit 46f482c

Browse files
committed
fix tests and merge the escrow function
1 parent b0ea50d commit 46f482c

7 files changed

Lines changed: 382 additions & 85 deletions

File tree

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ build_binary:
6060
# bazel build --platforms=@io_bazel_rules_go//go/toolchain:darwin_amd64 //:cmd:crypt-amd
6161
# bazel build --platforms=@io_bazel_rules_go//go/toolchain:darwin_arm //cmd:crypt-arm
6262
# tools/bazel_to_builddir.sh
63-
CGO_ENABLED=1 CC=/opt/homebrew/opt/llvm/bin/clang CXX=/opt/homebrew/opt/llvm/bin/clang++ GOOS=darwin GOARCH=arm64 go build -ldflags "-X main.version=${BUNDLE_VERSION}" -o build/checkin.arm64 cmd/main.go
64-
CGO_ENABLED=1 CC=/opt/homebrew/opt/llvm/bin/clang CXX=/opt/homebrew/opt/llvm/bin/clang++ GOOS=darwin GOARCH=amd64 go build -ldflags "-X main.version=${BUNDLE_VERSION}" -o build/checkin.amd64 cmd/main.go
63+
MACOSX_DEPLOYMENT_TARGET=13.0 CGO_ENABLED=1 CC=/opt/homebrew/opt/llvm/bin/clang CXX=/opt/homebrew/opt/llvm/bin/clang++ GOOS=darwin GOARCH=arm64 go build -ldflags "-X main.version=${BUNDLE_VERSION}" -o build/checkin.arm64 cmd/main.go
64+
MACOSX_DEPLOYMENT_TARGET=13.0 CGO_ENABLED=1 CC=/opt/homebrew/opt/llvm/bin/clang CXX=/opt/homebrew/opt/llvm/bin/clang++ GOOS=darwin GOARCH=amd64 go build -ldflags "-X main.version=${BUNDLE_VERSION}" -o build/checkin.amd64 cmd/main.go
6565
/usr/bin/lipo -create -output build/checkin build/checkin.arm64 build/checkin.amd64
6666
/bin/rm build/checkin.arm64
6767
/bin/rm build/checkin.amd64

pkg/authmechs/authemechs.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,12 @@ func checkMechsInDB(db AuthDB, mechList []string, indexMech string, indexOffset
5555
return reflect.DeepEqual(db.Mechanisms[insertIndex:insertIndex+len(mechList)], mechList)
5656
}
5757

58-
func setMechsInDB(db AuthDB, mechList []string, indexMech string, indexOffset int, add bool) AuthDB {
58+
func setMechsInDB(db AuthDB, mechList []string, indexMech string, indexOffset int) AuthDB {
59+
// Remove all the mechanisms that crypt ever added but are not needed anymore we'll re-add the ones we need
5960
db = removeMechsInDB(db, fv2MechsToRemove)
6061

61-
if add {
62-
insertIndex := indexOf(db.Mechanisms, indexMech) + indexOffset
63-
db.Mechanisms = insertMechsAtPosition(db.Mechanisms, mechList, insertIndex)
64-
}
62+
insertIndex := indexOf(db.Mechanisms, indexMech) + indexOffset
63+
db.Mechanisms = insertMechsAtPosition(db.Mechanisms, mechList, insertIndex)
6564

6665
return db
6766
}
@@ -114,7 +113,7 @@ func editAuthDB(r utils.Runner, add bool) error {
114113
return err
115114
}
116115

117-
d = setMechsInDB(d, fv2Mechs, fv2IndexMech, fv2IndexOffset, add)
116+
d = setMechsInDB(d, fv2Mechs, fv2IndexMech, fv2IndexOffset)
118117
data, err := plist.Marshal(d)
119118
if err != nil {
120119
return err

pkg/authmechs/authmechs_test.go

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,6 @@ func TestSetMechsInDB(t *testing.T) {
101101
mechList []string
102102
indexMech string
103103
indexOffset int
104-
add bool
105104
want AuthDB
106105
}{
107106
{
@@ -110,7 +109,6 @@ func TestSetMechsInDB(t *testing.T) {
110109
mechList: []string{"mech1", "mech2"},
111110
indexMech: "mech1",
112111
indexOffset: 1,
113-
add: true,
114112
want: AuthDB{Mechanisms: []string{"mech1", "mech2"}},
115113
},
116114
{
@@ -119,7 +117,6 @@ func TestSetMechsInDB(t *testing.T) {
119117
mechList: []string{"mech4", "mech5"},
120118
indexMech: "mech2",
121119
indexOffset: 1,
122-
add: true,
123120
want: AuthDB{Mechanisms: []string{"mech1", "mech2", "mech4", "mech5", "mech3"}},
124121
},
125122
{
@@ -128,32 +125,29 @@ func TestSetMechsInDB(t *testing.T) {
128125
mechList: []string{},
129126
indexMech: "mech2",
130127
indexOffset: 1,
131-
add: true,
132128
want: AuthDB{Mechanisms: []string{"mech1", "mech2", "mech3"}},
133129
},
134130
{
135-
name: "Test with non-empty db, add is false",
131+
name: "Test with non-empty db and mechList to add",
136132
db: AuthDB{Mechanisms: []string{"mech1", "mech2", "mech3"}},
137-
mechList: []string{"mech4", "mech3"},
133+
mechList: []string{"mech4", "mech5"},
138134
indexMech: "mech2",
139135
indexOffset: 1,
140-
add: false,
141-
want: AuthDB{Mechanisms: []string{"mech1", "mech2"}},
136+
want: AuthDB{Mechanisms: []string{"mech1", "mech2", "mech4", "mech5", "mech3"}},
142137
},
143138
{
144-
name: "Test with non-empty db, mechList is empty, add is false",
139+
name: "Test with non-empty db, empty mechList",
145140
db: AuthDB{Mechanisms: []string{"mech1", "mech2", "mech3"}},
146141
mechList: []string{},
147142
indexMech: "mech2",
148143
indexOffset: 1,
149-
add: false,
150144
want: AuthDB{Mechanisms: []string{"mech1", "mech2", "mech3"}},
151145
},
152146
}
153147

154148
for _, tt := range tests {
155149
t.Run(tt.name, func(t *testing.T) {
156-
got := setMechsInDB(tt.db, tt.mechList, tt.indexMech, tt.indexOffset, tt.add)
150+
got := setMechsInDB(tt.db, tt.mechList, tt.indexMech, tt.indexOffset)
157151
assert.Equal(t, tt.want, got)
158152
})
159153
}

pkg/checkin/escrow.go

Lines changed: 34 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -130,13 +130,7 @@ func RunEscrow(r utils.Runner, p pref.PrefInterface) error {
130130
return errors.Wrap(err, "failed to get mTLS common name for escrow")
131131
}
132132

133-
if mTLScommonName != "" {
134-
// we will use mTLS for escrow, as well as native go http client
135-
keyRotated, err = escrowWithMTLS(cryptData, r, p, mTLScommonName)
136-
} else {
137-
// escrow using curl if mTLS is not configured
138-
keyRotated, err = escrowKey(cryptData, r, p)
139-
}
133+
keyRotated, err = escrowKey(cryptData, r, p, mTLScommonName)
140134
if err != nil {
141135
return errors.Wrap(err, "escrow operation failed")
142136
}
@@ -546,44 +540,60 @@ func runCurl(configFile string, r utils.Runner, p pref.PrefInterface) (string, e
546540
return string(out), nil
547541
}
548542

549-
// escrowKey attempts to escrow a key by sending data to a server URL built from preferences.
550-
// It logs the process and handles errors appropriately.
543+
// escrowKey attempts to escrow a key to the server, using either mTLS or curl based on configuration.
544+
// It builds the check-in URL, constructs the form data, and sends the escrow request using
545+
// the appropriate method based on whether a CommonNameForEscrow is configured.
551546
//
552547
// Parameters:
553-
// - plist: CryptData containing the data to be sent.
554-
// - r: utils.Runner to execute commands.
555-
// - p: pref.PrefInterface to retrieve preferences.
548+
// - plist: CryptData containing the data to be sent
549+
// - r: utils.Runner interface for executing commands
550+
// - p: pref.PrefInterface for accessing preferences
551+
// - mTLScommonName: Optional common name for mTLS authentication. If empty, curl will be used.
556552
//
557553
// Returns:
558-
// - bool: indicating if the key rotation was initiated by the server.
559-
// - error: if any error occurs during the process.
560-
func escrowKey(plist CryptData, r utils.Runner, p pref.PrefInterface) (bool, error) {
554+
// - bool: Indicates if the key was rotated as part of the escrow process
555+
// - error: Any error encountered during the process
556+
func escrowKey(plist CryptData, r utils.Runner, p pref.PrefInterface, mTLScommonName string) (bool, error) {
561557
log.Println("Attempting to Escrow Key...")
562-
// serverURL, err := p.GetString("ServerURL")
563-
// if err != nil {
564-
// return errors.Wrap(err, "failed to get server URL")
565-
// }
558+
566559
theURL, err := buildCheckinURL(p)
567560
if err != nil {
568561
return false, errors.Wrap(err, "failed to build checkin URL")
569562
}
570563

564+
// Build form data
571565
data, err := buildData(plist, r)
572566
if err != nil {
573567
return false, errors.Wrap(err, "failed to build data")
574568
}
575-
configFile := utils.BuildCurlConfigFile(map[string]string{"url": theURL, "data": data})
576-
output, err := runCurl(configFile, r, p)
577-
if err != nil {
578-
return false, errors.Wrap(err, "failed to run curl")
569+
570+
var responseBody string
571+
572+
// Determine whether to use mTLS or curl based on whether a common name is provided
573+
if mTLScommonName != "" {
574+
log.Printf("Using mTLS for escrow with common name: %s", mTLScommonName)
575+
body, err := sendRequest(theURL, data, mTLScommonName)
576+
if err != nil {
577+
return false, errors.Wrap(err, "failed to send request with mTLS")
578+
}
579+
responseBody = string(body)
580+
} else {
581+
log.Println("Using curl for escrow")
582+
configFile := utils.BuildCurlConfigFile(map[string]string{"url": theURL, "data": data})
583+
output, err := runCurl(configFile, r, p)
584+
if err != nil {
585+
return false, errors.Wrap(err, "failed to run curl")
586+
}
587+
responseBody = output
579588
}
580589

581590
log.Println("Key escrow successful.")
582591

583-
keyRotated, err := serverInitiatedRotation(output, r, p)
592+
keyRotated, err := serverInitiatedRotation(responseBody, r, p)
584593
if err != nil {
585594
return false, errors.Wrap(err, "serverInitiatedRotation")
586595
}
596+
587597
return keyRotated, nil
588598
}
589599

@@ -773,48 +783,6 @@ func getRecoveryKey(keyLocation string, p pref.PrefInterface) (string, error) {
773783
return key.RecoveryKey, nil
774784
}
775785

776-
// escrowWithMTLS attempts to escrow a key using mTLS (mutual TLS) authentication.
777-
// It builds the check-in URL, constructs the form data, creates an HTTP POST request,
778-
// and sends it using an mTLS client. The function checks the response status and
779-
// processes the response body to determine if the key escrow was successful.
780-
//
781-
// Parameters:
782-
// - plist: CryptData containing the data to be sent.
783-
// - r: utils.Runner interface for executing commands.
784-
// - p: pref.PrefInterface for accessing preferences.
785-
//
786-
// Returns:
787-
// - bool: Indicates if the key was rotated as part of the escrow process.
788-
// - error: Any error encountered during the process.
789-
func escrowWithMTLS(plist CryptData, r utils.Runner, p pref.PrefInterface, commonName string) (bool, error) {
790-
log.Println("Attempting to Escrow Key...")
791-
792-
theURL, err := buildCheckinURL(p)
793-
if err != nil {
794-
return false, errors.Wrap(err, "failed to build checkin URL")
795-
}
796-
797-
// Build form data
798-
data, err := buildData(plist, r)
799-
if err != nil {
800-
return false, errors.Wrap(err, "failed to build data")
801-
}
802-
803-
body, err := sendRequest(theURL, data, commonName)
804-
if err != nil {
805-
return false, errors.Wrap(err, "failed to send request")
806-
}
807-
808-
log.Println("Key escrow successful.")
809-
810-
keyRotated, err := serverInitiatedRotation(string(body), r, p)
811-
if err != nil {
812-
return false, errors.Wrap(err, "serverInitiatedRotation")
813-
}
814-
815-
return keyRotated, nil
816-
}
817-
818786
// sendRequest sends an HTTP POST request to the specified URL with the given data
819787
// and uses mTLS (mutual TLS) for authentication with the provided common name.
820788
// It returns the response body as a byte slice or an error if the request fails.

0 commit comments

Comments
 (0)