Skip to content
Snippets Groups Projects
Commit 09891c3a authored by Josh Brooks's avatar Josh Brooks
Browse files

Remove hardcoded URL for NDF downloading

parent 17e0d275
No related branches found
No related tags found
2 merge requests!117Release,!74Implement bindings for downloading and verifying signed partial NDF
...@@ -18,33 +18,6 @@ import ( ...@@ -18,33 +18,6 @@ import (
"net/http" "net/http"
) )
// ndfUrl is a hardcoded url to a bucket containing the signed NDF message.
const ndfUrl = `https://elixxir-bins.s3.us-west-1.amazonaws.com/ndf/default.json`
// DownloadAndVerifySignedNdf retrieves the NDF from a hardcoded bucket URL.
// The NDF is processed into a protobuf containing a signature which
// is verified using the cert string passed in. The NDF is returned as marshaled
// byte data which may be used to start a client.
func DownloadAndVerifySignedNdf(cert string) ([]byte, error) {
// Build a request for the file
resp, err := http.Get(ndfUrl)
if err != nil {
return nil, errors.WithMessagef(err, "Failed to retrieve "+
"NDF from %s", ndfUrl)
}
defer resp.Body.Close()
// Download contents of the file
signedNdfEncoded, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, errors.WithMessage(err, "Failed to read signed "+
"NDF response request")
}
// Process the download NDF and return the marshaled NDF
return processAndVerifySignedNdf(signedNdfEncoded, cert)
}
// DownloadAndVerifySignedNdfWithUrl retrieves the NDF from a specified URL. // DownloadAndVerifySignedNdfWithUrl retrieves the NDF from a specified URL.
// The NDF is processed into a protobuf containing a signature which // The NDF is processed into a protobuf containing a signature which
// is verified using the cert string passed in. The NDF is returned as marshaled // is verified using the cert string passed in. The NDF is returned as marshaled
......
...@@ -51,11 +51,11 @@ pipz4Cfpkoc1Gc8xx91iBsWYBpqu4p7SXDU= ...@@ -51,11 +51,11 @@ pipz4Cfpkoc1Gc8xx91iBsWYBpqu4p7SXDU=
-----END CERTIFICATE----- -----END CERTIFICATE-----
` `
// Unit test: Download and verify NDF from hosted location. // Unit Test: Call DownloadAndVerifySignedNdfWithUrl with a specified URL.
// Ensure validity by unmarshalling NDF and checking the scheduling's cert. // Ensure validity by unmarshalling NDF and checking the scheduling's cert.
func TestDownloadSignedNdf(t *testing.T) { func TestDownloadSignedNdfWithUrl(t *testing.T) {
// Download and verify the ndf // Download and verify the cert with the specified URL
content, err := DownloadAndVerifySignedNdf(testCert) content, err := DownloadAndVerifySignedNdfWithUrl("https://elixxir-bins.s3.us-west-1.amazonaws.com/ndf/default.json", testCert)
if err != nil { if err != nil {
t.Errorf("Failed to download signed NDF: %v", err) t.Errorf("Failed to download signed NDF: %v", err)
} }
...@@ -71,42 +71,21 @@ func TestDownloadSignedNdf(t *testing.T) { ...@@ -71,42 +71,21 @@ func TestDownloadSignedNdf(t *testing.T) {
if strings.Compare(downloadedNdf.Registration.TlsCertificate, testCert) != 0 { if strings.Compare(downloadedNdf.Registration.TlsCertificate, testCert) != 0 {
t.Fatalf("Unexpected NDF downloaded, has the spec changed?") t.Fatalf("Unexpected NDF downloaded, has the spec changed?")
} }
} }
// Error case: Pass in the incorrect cert forcing a verification failure. // Error case: Pass in the incorrect cert forcing a verification failure.
func TestDownloadSignedNdf_Fail(t *testing.T) { func TestDownloadSignedNdfWithUrl_BadCert(t *testing.T) {
// Load an unintended cert // Load an unintended cert
badCert, err := utils.ReadFile(testkeys.GetGatewayCertPath()) badCert, err := utils.ReadFile(testkeys.GetGatewayCertPath())
if err != nil { if err != nil {
t.Fatalf("Failed to read test certificate: %v", err) t.Fatalf("Failed to read test certificate: %v", err)
} }
// Download and verify with unintended cert
_, err = DownloadAndVerifySignedNdf(string(badCert)) // Download and attempt to verify with unintended cert
_, err = DownloadAndVerifySignedNdfWithUrl("https://elixxir-bins.s3.us-west-1.amazonaws.com/ndf/default.json",
string(badCert))
if err == nil { if err == nil {
t.Fatalf("Expected failure, should not be able to verify with " + t.Fatalf("Expected failure, should not be able to verify with " +
"bad certificate") "bad certificate")
} }
} }
// Unit Test: Call DownloadAndVerifySignedNdfWithUrl with a specified URL.
// Ensure validity by unmarshalling NDF and checking the scheduling's cert.
func TestDownloadSignedNdfWithUrl(t *testing.T) {
// todo: write test once a proper URL can be passed in
content, err := DownloadAndVerifySignedNdfWithUrl(ndfUrl, testCert)
if err != nil {
t.Errorf("Failed to download signed NDF: %v", err)
}
fmt.Printf("content: %s\n", string(content))
// Check that it is a marshallable NDF
downloadedNdf, err := ndf.Unmarshal(content)
if err != nil {
t.Fatalf("Failed to unmarshal downloaded NDF: %v", err)
}
// Check validity of NDF
if strings.Compare(downloadedNdf.Registration.TlsCertificate, testCert) != 0 {
t.Fatalf("Unexpected NDF downloaded, has the spec changed?")
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment