blob: e5334f052045f5a148920d0e5a82cc8190f5c443 [file] [log] [blame]
jrperritt3d966162016-06-06 14:08:54 -05001package testing
Rickard von Essenc3d49b72016-02-16 20:59:18 +01002
3import (
4 "crypto/rsa"
5 "encoding/json"
6 "fmt"
7 "testing"
8
jrperritt2f93a632016-04-13 15:41:20 -05009 "github.com/gophercloud/gophercloud"
jrperritt3d966162016-06-06 14:08:54 -050010 "github.com/gophercloud/gophercloud/openstack/compute/v2/servers"
jrperritt2f93a632016-04-13 15:41:20 -050011 th "github.com/gophercloud/gophercloud/testhelper"
Rickard von Essenc3d49b72016-02-16 20:59:18 +010012 "golang.org/x/crypto/ssh"
13)
14
15// Fail - No password in JSON.
16func TestExtractPassword_no_pwd_data(t *testing.T) {
17
18 var dejson interface{}
19 err := json.Unmarshal([]byte(`{ "Crappy data": ".-.-." }`), &dejson)
20 if err != nil {
21 t.Fatalf("%s", err)
22 }
jrperritt3d966162016-06-06 14:08:54 -050023 resp := servers.GetPasswordResult{gophercloud.Result{Body: dejson}}
Rickard von Essenc3d49b72016-02-16 20:59:18 +010024
25 pwd, err := resp.ExtractPassword(nil)
26 th.AssertEquals(t, pwd, "")
27}
28
29// Ok - return encrypted password when no private key is given.
30func TestExtractPassword_encrypted_pwd(t *testing.T) {
31
32 var dejson interface{}
33 sejson := []byte(`{"password":"PP8EnwPO9DhEc8+O/6CKAkPF379mKsUsfFY6yyw0734XXvKsSdV9KbiHQ2hrBvzeZxtGMrlFaikVunCRizyLLWLMuOi4hoH+qy9F9sQid61gQIGkxwDAt85d/7Eau2/KzorFnZhgxArl7IiqJ67X6xjKkR3zur+Yp3V/mtVIehpPYIaAvPbcp2t4mQXl1I9J8yrQfEZOctLL1L4heDEVXnxvNihVLK6pivlVggp6SZCtjj9cduZGrYGsxsOCso1dqJQr7GCojfwvuLOoG0OYwEGuWVTZppxWxi/q1QgeHFhGKA5QUXlz7pS71oqpjYsTeViuHnfvlqb5TVYZpQ1haw=="}`)
34
35 err := json.Unmarshal(sejson, &dejson)
36 fmt.Printf("%v\n", dejson)
37 if err != nil {
38 t.Fatalf("%s", err)
39 }
jrperritt3d966162016-06-06 14:08:54 -050040 resp := servers.GetPasswordResult{gophercloud.Result{Body: dejson}}
Rickard von Essenc3d49b72016-02-16 20:59:18 +010041
42 pwd, err := resp.ExtractPassword(nil)
43 th.AssertNoErr(t, err)
44 th.AssertEquals(t, "PP8EnwPO9DhEc8+O/6CKAkPF379mKsUsfFY6yyw0734XXvKsSdV9KbiHQ2hrBvzeZxtGMrlFaikVunCRizyLLWLMuOi4hoH+qy9F9sQid61gQIGkxwDAt85d/7Eau2/KzorFnZhgxArl7IiqJ67X6xjKkR3zur+Yp3V/mtVIehpPYIaAvPbcp2t4mQXl1I9J8yrQfEZOctLL1L4heDEVXnxvNihVLK6pivlVggp6SZCtjj9cduZGrYGsxsOCso1dqJQr7GCojfwvuLOoG0OYwEGuWVTZppxWxi/q1QgeHFhGKA5QUXlz7pS71oqpjYsTeViuHnfvlqb5TVYZpQ1haw==", pwd)
45}
46
47// Ok - return decrypted password when private key is given.
48// Decrytion can be verified by:
49// echo "<enc_pwd>" | base64 -D | openssl rsautl -decrypt -inkey <privateKey.pem>
50func TestExtractPassword_decrypted_pwd(t *testing.T) {
51
52 privateKey, err := ssh.ParseRawPrivateKey([]byte(`
53-----BEGIN RSA PRIVATE KEY-----
54MIIEpQIBAAKCAQEAo1ODZgwMVdTJYim9UYuYhowoPMhGEuV5IRZjcJ315r7RBSC+
55yEiBb1V+jhf+P8fzAyU35lkBzZGDr7E3jxSesbOuYT8cItQS4ErUnI1LGuqvMxwv
56X3GMyE/HmOcaiODF1XZN3Ur5pMJdVknnmczgUsW0hT98Udrh3MQn9WSuh/6LRy6+
57x1QsKHOCLFPnkhWa3LKyxmpQq/Gvhz+6NLe+gt8MFullA5mKQxBJ/K6laVHeaMlw
58JG3GCX0EZhRlvzoV8koIBKZtbKFolFr8ZtxBm3R5LvnyrtOvp22sa+xeItUT5kG1
59ZnbGNdK87o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lyYyjD6N9JE2KqEyKoh6J+8F7sXsqW7CqRRDfQX
76mKNfey0CgYEAxcEoNoADN2hRl7qY9rbQfVvQb3RkoQkdHhl9gpLFCcV32IP8R4xg
7709NbQK5OmgcIuZhLVNzTmUHJbabEGeXqIFIV0DsqECAt3WzbDyKQO23VJysFD46c
78KSde3I0ybDz7iS2EtceKB7m4C0slYd+oBkm4efuF00rCOKDwpFq45m0=
79-----END RSA PRIVATE KEY-----
80`))
81 if err != nil {
82 t.Fatalf("Error parsing private key: %s\n", err)
83 }
84
85 var dejson interface{}
86 sejson := []byte(`{"password":"PP8EnwPO9DhEc8+O/6CKAkPF379mKsUsfFY6yyw0734XXvKsSdV9KbiHQ2hrBvzeZxtGMrlFaikVunCRizyLLWLMuOi4hoH+qy9F9sQid61gQIGkxwDAt85d/7Eau2/KzorFnZhgxArl7IiqJ67X6xjKkR3zur+Yp3V/mtVIehpPYIaAvPbcp2t4mQXl1I9J8yrQfEZOctLL1L4heDEVXnxvNihVLK6pivlVggp6SZCtjj9cduZGrYGsxsOCso1dqJQr7GCojfwvuLOoG0OYwEGuWVTZppxWxi/q1QgeHFhGKA5QUXlz7pS71oqpjYsTeViuHnfvlqb5TVYZpQ1haw=="}`)
87
88 err = json.Unmarshal(sejson, &dejson)
89 fmt.Printf("%v\n", dejson)
90 if err != nil {
91 t.Fatalf("%s", err)
92 }
jrperritt3d966162016-06-06 14:08:54 -050093 resp := servers.GetPasswordResult{gophercloud.Result{Body: dejson}}
Rickard von Essenc3d49b72016-02-16 20:59:18 +010094
95 pwd, err := resp.ExtractPassword(privateKey.(*rsa.PrivateKey))
96 th.AssertNoErr(t, err)
97 th.AssertEquals(t, "ruZKK0tqxRfYm5t7lSJq", pwd)
98}