github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/identity/v3/ec2tokens/testing/requests_test.go (about)

     1  package testing
     2  
     3  import (
     4  	"context"
     5  	"encoding/hex"
     6  	"fmt"
     7  	"net/http"
     8  	"testing"
     9  	"time"
    10  
    11  	"github.com/vnpaycloud-console/gophercloud/v2"
    12  	"github.com/vnpaycloud-console/gophercloud/v2/openstack/identity/v3/ec2tokens"
    13  	"github.com/vnpaycloud-console/gophercloud/v2/openstack/identity/v3/tokens"
    14  	tokens_testing "github.com/vnpaycloud-console/gophercloud/v2/openstack/identity/v3/tokens/testing"
    15  	th "github.com/vnpaycloud-console/gophercloud/v2/testhelper"
    16  )
    17  
    18  // authTokenPost verifies that providing certain AuthOptions and Scope results in an expected JSON structure.
    19  func authTokenPost(t *testing.T, options ec2tokens.AuthOptions, requestJSON string) {
    20  	th.SetupHTTP()
    21  	defer th.TeardownHTTP()
    22  
    23  	client := gophercloud.ServiceClient{
    24  		ProviderClient: &gophercloud.ProviderClient{},
    25  		Endpoint:       th.Endpoint(),
    26  	}
    27  
    28  	th.Mux.HandleFunc("/ec2tokens", func(w http.ResponseWriter, r *http.Request) {
    29  		th.TestMethod(t, r, "POST")
    30  		th.TestHeader(t, r, "Content-Type", "application/json")
    31  		th.TestHeader(t, r, "Accept", "application/json")
    32  		th.TestJSONRequest(t, r, requestJSON)
    33  
    34  		w.WriteHeader(http.StatusOK)
    35  		fmt.Fprint(w, tokens_testing.TokenOutput)
    36  	})
    37  
    38  	expected := &tokens.Token{
    39  		ExpiresAt: time.Date(2017, 6, 3, 2, 19, 49, 0, time.UTC),
    40  	}
    41  
    42  	actual, err := ec2tokens.Create(context.TODO(), &client, &options).Extract()
    43  	th.AssertNoErr(t, err)
    44  	th.CheckDeepEquals(t, expected, actual)
    45  }
    46  
    47  func TestCreateV2(t *testing.T) {
    48  	credentials := ec2tokens.AuthOptions{
    49  		Access: "a7f1e798b7c2417cba4a02de97dc3cdc",
    50  		Host:   "localhost",
    51  		Path:   "/",
    52  		Secret: "18f4f6761ada4e3795fa5273c30349b9",
    53  		Verb:   "GET",
    54  		// this should be removed from JSON request
    55  		BodyHash: new(string),
    56  		// this should be removed from JSON request
    57  		Headers: map[string]string{
    58  			"Foo": "Bar",
    59  		},
    60  		Params: map[string]string{
    61  			"Action":           "Test",
    62  			"SignatureMethod":  "HmacSHA256",
    63  			"SignatureVersion": "2",
    64  		},
    65  	}
    66  	authTokenPost(t, credentials, `{
    67      "credentials": {
    68          "access": "a7f1e798b7c2417cba4a02de97dc3cdc",
    69          "host": "localhost",
    70          "params": {
    71              "Action": "Test",
    72              "SignatureMethod": "HmacSHA256",
    73              "SignatureVersion": "2"
    74          },
    75          "path": "/",
    76          "signature": "Up+MbVbbrvdR5FRkUz+n3nc+VW6xieuN50wh6ONEJ4w=",
    77          "verb": "GET"
    78      }
    79  }`)
    80  }
    81  
    82  func TestCreateV4(t *testing.T) {
    83  	bodyHash := "foo"
    84  	credentials := ec2tokens.AuthOptions{
    85  		Access:    "a7f1e798b7c2417cba4a02de97dc3cdc",
    86  		BodyHash:  &bodyHash,
    87  		Timestamp: new(time.Time),
    88  		Region:    "region1",
    89  		Service:   "ec2",
    90  		Path:      "/",
    91  		Secret:    "18f4f6761ada4e3795fa5273c30349b9",
    92  		Verb:      "GET",
    93  		Headers: map[string]string{
    94  			"Host": "localhost",
    95  		},
    96  		Params: map[string]string{
    97  			"Action": "Test",
    98  		},
    99  	}
   100  	authTokenPost(t, credentials, `{
   101      "credentials": {
   102          "access": "a7f1e798b7c2417cba4a02de97dc3cdc",
   103          "body_hash": "foo",
   104          "host": "",
   105          "headers": {
   106              "Host": "localhost",
   107              "Authorization": "AWS4-HMAC-SHA256 Credential=a7f1e798b7c2417cba4a02de97dc3cdc/00010101/region1/ec2/aws4_request, SignedHeaders=, Signature=f36f79118f75d7d6ec86ead9a61679cbdcf94c0cbfe5e9cf2407e8406aa82028",
   108              "X-Amz-Date": "00010101T000000Z"
   109           },
   110          "params": {
   111              "Action": "Test"
   112          },
   113          "path": "/",
   114          "signature": "f36f79118f75d7d6ec86ead9a61679cbdcf94c0cbfe5e9cf2407e8406aa82028",
   115          "verb": "GET"
   116      }
   117  }`)
   118  }
   119  
   120  func TestCreateV4Empty(t *testing.T) {
   121  	credentials := ec2tokens.AuthOptions{
   122  		Access:    "a7f1e798b7c2417cba4a02de97dc3cdc",
   123  		Secret:    "18f4f6761ada4e3795fa5273c30349b9",
   124  		BodyHash:  new(string),
   125  		Timestamp: new(time.Time),
   126  	}
   127  	authTokenPost(t, credentials, `{
   128      "credentials": {
   129          "access": "a7f1e798b7c2417cba4a02de97dc3cdc",
   130          "body_hash": "",
   131          "host": "",
   132          "headers": {
   133              "Authorization": "AWS4-HMAC-SHA256 Credential=a7f1e798b7c2417cba4a02de97dc3cdc/00010101///aws4_request, SignedHeaders=, Signature=140a31abf1efe93a607dcac6cd8f66887b86d2bc8f712c290d9aa06edf428608",
   134              "X-Amz-Date": "00010101T000000Z"
   135          },
   136          "params": {},
   137          "path": "",
   138          "signature": "140a31abf1efe93a607dcac6cd8f66887b86d2bc8f712c290d9aa06edf428608",
   139          "verb": ""
   140      }
   141  }`)
   142  }
   143  
   144  func TestCreateV4Headers(t *testing.T) {
   145  	credentials := ec2tokens.AuthOptions{
   146  		Access:    "a7f1e798b7c2417cba4a02de97dc3cdc",
   147  		BodyHash:  new(string),
   148  		Timestamp: new(time.Time),
   149  		Region:    "region1",
   150  		Service:   "ec2",
   151  		Path:      "/",
   152  		Secret:    "18f4f6761ada4e3795fa5273c30349b9",
   153  		Verb:      "GET",
   154  		Headers: map[string]string{
   155  			"Foo":  "Bar",
   156  			"Host": "localhost",
   157  		},
   158  		Params: map[string]string{
   159  			"Action": "Test",
   160  		},
   161  	}
   162  	authTokenPost(t, credentials, `{
   163      "credentials": {
   164          "access": "a7f1e798b7c2417cba4a02de97dc3cdc",
   165          "body_hash": "",
   166          "host": "",
   167          "headers": {
   168              "Foo": "Bar",
   169              "Host": "localhost",
   170              "Authorization": "AWS4-HMAC-SHA256 Credential=a7f1e798b7c2417cba4a02de97dc3cdc/00010101/region1/ec2/aws4_request, SignedHeaders=, Signature=f5cd6995be98e5576a130b30cca277375f10439217ea82169aa8386e83965611",
   171              "X-Amz-Date": "00010101T000000Z"
   172          },
   173          "params": {
   174              "Action": "Test"
   175          },
   176          "path": "/",
   177          "signature": "f5cd6995be98e5576a130b30cca277375f10439217ea82169aa8386e83965611",
   178          "verb": "GET"
   179      }
   180  }`)
   181  }
   182  
   183  func TestCreateV4WithSignature(t *testing.T) {
   184  	credentials := ec2tokens.AuthOptions{
   185  		Access:    "a7f1e798b7c2417cba4a02de97dc3cdc",
   186  		BodyHash:  new(string),
   187  		Path:      "/",
   188  		Signature: "f5cd6995be98e5576a130b30cca277375f10439217ea82169aa8386e83965611",
   189  		Verb:      "GET",
   190  		Headers: map[string]string{
   191  			"Foo":           "Bar",
   192  			"Host":          "localhost",
   193  			"Authorization": "AWS4-HMAC-SHA256 Credential=a7f1e798b7c2417cba4a02de97dc3cdc/00010101/region1/ec2/aws4_request, SignedHeaders=, Signature=f5cd6995be98e5576a130b30cca277375f10439217ea82169aa8386e83965611",
   194  			"X-Amz-Date":    "00010101T000000Z",
   195  		},
   196  		Params: map[string]string{
   197  			"Action": "Test",
   198  		},
   199  	}
   200  	authTokenPost(t, credentials, `{
   201      "credentials": {
   202          "access": "a7f1e798b7c2417cba4a02de97dc3cdc",
   203          "body_hash": "",
   204          "host": "",
   205          "headers": {
   206              "Foo": "Bar",
   207              "Host": "localhost",
   208              "Authorization": "AWS4-HMAC-SHA256 Credential=a7f1e798b7c2417cba4a02de97dc3cdc/00010101/region1/ec2/aws4_request, SignedHeaders=, Signature=f5cd6995be98e5576a130b30cca277375f10439217ea82169aa8386e83965611",
   209              "X-Amz-Date": "00010101T000000Z"
   210          },
   211          "params": {
   212              "Action": "Test"
   213          },
   214          "path": "/",
   215          "signature": "f5cd6995be98e5576a130b30cca277375f10439217ea82169aa8386e83965611",
   216          "verb": "GET"
   217      }
   218  }`)
   219  }
   220  
   221  func TestEC2CredentialsBuildCanonicalQueryStringV2(t *testing.T) {
   222  	params := map[string]string{
   223  		"Action": "foo",
   224  		"Value":  "bar",
   225  	}
   226  	expected := "Action=foo&Value=bar"
   227  	th.CheckEquals(t, expected, ec2tokens.EC2CredentialsBuildCanonicalQueryStringV2(params))
   228  }
   229  
   230  func TestEC2CredentialsBuildStringToSignV2(t *testing.T) {
   231  	opts := ec2tokens.AuthOptions{
   232  		Verb: "GET",
   233  		Host: "localhost",
   234  		Path: "/",
   235  		Params: map[string]string{
   236  			"Action": "foo",
   237  			"Value":  "bar",
   238  		},
   239  	}
   240  	expected := []byte("GET\nlocalhost\n/\nAction=foo&Value=bar")
   241  	th.CheckDeepEquals(t, expected, ec2tokens.EC2CredentialsBuildStringToSignV2(opts))
   242  }
   243  
   244  func TestEC2CredentialsBuildCanonicalQueryStringV4(t *testing.T) {
   245  	params := map[string]string{
   246  		"Action": "foo",
   247  		"Value":  "bar",
   248  	}
   249  	expected := "Action=foo&Value=bar"
   250  	th.CheckEquals(t, expected, ec2tokens.EC2CredentialsBuildCanonicalQueryStringV4("foo", params))
   251  	th.CheckEquals(t, "", ec2tokens.EC2CredentialsBuildCanonicalQueryStringV4("POST", params))
   252  }
   253  
   254  func TestEC2CredentialsBuildCanonicalHeadersV4(t *testing.T) {
   255  	headers := map[string]string{
   256  		"Foo": "bar",
   257  		"Baz": "qux",
   258  	}
   259  	signedHeaders := "foo;baz"
   260  	expected := "foo:bar\nbaz:qux\n"
   261  	th.CheckEquals(t, expected, ec2tokens.EC2CredentialsBuildCanonicalHeadersV4(headers, signedHeaders))
   262  }
   263  
   264  func TestEC2CredentialsBuildSignatureKeyV4(t *testing.T) {
   265  	expected := "246626bd815b0a0cae4bedc3f4e124ca25e208cd75fd812d836aeae184de038a"
   266  	th.CheckEquals(t, expected, hex.EncodeToString((ec2tokens.EC2CredentialsBuildSignatureKeyV4("foo", "bar", "baz", time.Time{}))))
   267  }
   268  
   269  func TestEC2CredentialsBuildSignatureV4(t *testing.T) {
   270  	opts := ec2tokens.AuthOptions{
   271  		Verb: "GET",
   272  		Path: "/",
   273  		Headers: map[string]string{
   274  			"Host": "localhost",
   275  		},
   276  		Params: map[string]string{
   277  			"Action": "foo",
   278  			"Value":  "bar",
   279  		},
   280  	}
   281  	expected := "6a5febe41427bf601f0ae7c34dbb0fd67094776138b03fb8e65783d733d302a5"
   282  
   283  	date := time.Time{}
   284  	stringToSign := ec2tokens.EC2CredentialsBuildStringToSignV4(opts, "host", "foo", date)
   285  	key := ec2tokens.EC2CredentialsBuildSignatureKeyV4("", "", "", date)
   286  
   287  	th.CheckEquals(t, expected, ec2tokens.EC2CredentialsBuildSignatureV4(key, stringToSign))
   288  }