Skip to content
This repository was archived by the owner on Aug 1, 2023. It is now read-only.

Commit 5b8bbff

Browse files
Add Nova get-password support
Add support to get a encrypted administrative password for a server through a GET on: /v2.1/{tenant_id}/servers/{server_id}/os-server-password optionally decrypting the password if a private key is supplied. The same operation with OpenStack CLI is done with: nova get-password <server_id> [private_key.pem]
1 parent 6769c3b commit 5b8bbff

File tree

6 files changed

+91
-4
lines changed

6 files changed

+91
-4
lines changed

openstack/compute/v2/servers/fixtures.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,12 @@ const SingleServerBody = `
235235
}
236236
`
237237

238+
const ServerPasswordBody = `
239+
{
240+
"password": "xlozO3wLCBRWAa2yDjCCVx8vwNPypxnypmRYDa/zErlQ+EzPe1S/Gz6nfmC52mOlOSCRuUOmG7kqqgejPof6M7bOezS387zjq4LSvvwp28zUknzy4YzfFGhnHAdai3TxUJ26pfQCYrq8UTzmKF2Bq8ioSEtVVzM0A96pDh8W2i7BOz6MdoiVyiev/I1K2LsuipfxSJR7Wdke4zNXJjHHP2RfYsVbZ/k9ANu+Nz4iIH8/7Cacud/pphH7EjrY6a4RZNrjQskrhKYed0YERpotyjYk1eDtRe72GrSiXteqCM4biaQ5w3ruS+AcX//PXk3uJ5kC7d67fPXaVz4WaQRYMg=="
241+
}
242+
`
243+
238244
var (
239245
// ServerHerp is a Server struct that should correspond to the first result in ServerListBody.
240246
ServerHerp = Server{
@@ -399,8 +405,8 @@ func HandleServerDeletionSuccessfully(t *testing.T) {
399405
})
400406
}
401407

402-
// HandleAdminPasswordChangeSuccessfully sets up the test server to respond to a server password
403-
// change request.
408+
// HandleServerForceDeletionSuccessfully sets up the test server to respond to a server force deletion
409+
// request.
404410
func HandleServerForceDeletionSuccessfully(t *testing.T) {
405411
th.Mux.HandleFunc("/servers/asdfasdfasdf/action", func(w http.ResponseWriter, r *http.Request) {
406412
th.TestMethod(t, r, "POST")
@@ -674,3 +680,13 @@ func HandleCreateServerImageSuccessfully(t *testing.T) {
674680
})
675681
}
676682

683+
// HandlePasswordGetSuccessfully sets up the test server to respond to a password Get request.
684+
func HandlePasswordGetSuccessfully(t *testing.T) {
685+
th.Mux.HandleFunc("/servers/1234asdf/os-server-password", func(w http.ResponseWriter, r *http.Request) {
686+
th.TestMethod(t, r, "GET")
687+
th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
688+
th.TestHeader(t, r, "Accept", "application/json")
689+
690+
fmt.Fprintf(w, ServerPasswordBody)
691+
})
692+
}

openstack/compute/v2/servers/requests.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -861,3 +861,12 @@ func IDFromName(client *gophercloud.ServiceClient, name string) (string, error)
861861
return "", fmt.Errorf("Found %d servers matching %s", serverCount, name)
862862
}
863863
}
864+
865+
// GetPassword makes a request agains the nova API to get the encrypted administrative password.
866+
func GetPassword(client *gophercloud.ServiceClient, serverId string) GetPasswordResult {
867+
var res GetPasswordResult
868+
_, res.Err = client.Request("GET", passwordURL(client, serverId), gophercloud.RequestOpts{
869+
JSONResponse: &res.Body,
870+
})
871+
return res
872+
}

openstack/compute/v2/servers/requests_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,15 @@ func TestChangeServerAdminPassword(t *testing.T) {
124124
th.AssertNoErr(t, res.Err)
125125
}
126126

127+
func TestGetPassword(t *testing.T) {
128+
th.SetupHTTP()
129+
defer th.TeardownHTTP()
130+
HandlePasswordGetSuccessfully(t)
131+
132+
res := GetPassword(client.ServiceClient(), "1234asdf")
133+
th.AssertNoErr(t, res.Err)
134+
}
135+
127136
func TestRebootServer(t *testing.T) {
128137
th.SetupHTTP()
129138
defer th.TeardownHTTP()

openstack/compute/v2/servers/results.go

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package servers
22

33
import (
4-
"reflect"
4+
"crypto/rsa"
5+
"encoding/base64"
56
"fmt"
6-
"path"
77
"net/url"
8+
"path"
9+
"reflect"
810

911
"github.com/mitchellh/mapstructure"
1012
"github.com/rackspace/gophercloud"
@@ -82,6 +84,47 @@ type CreateImageResult struct {
8284
gophercloud.Result
8385
}
8486

87+
// GetPasswordResult represent the result of a get os-server-password operation.
88+
type GetPasswordResult struct {
89+
gophercloud.Result
90+
}
91+
92+
// ExtractPassword gets the encrypted password.
93+
// If privateKey != nil the password is decrypted with the private key.
94+
// If privateKey == nil the encrypted password is returned and can be decrypted with:
95+
// echo '<pwd>' | base64 -D | openssl rsautl -decrypt -inkey <private_key>
96+
func (r GetPasswordResult) ExtractPassword(privateKey *rsa.PrivateKey) (string, error) {
97+
98+
if r.Err != nil {
99+
return "", r.Err
100+
}
101+
102+
var response struct {
103+
Password string `mapstructure:"password"`
104+
}
105+
106+
err := mapstructure.Decode(r.Body, &response)
107+
if err == nil && privateKey != nil && response.Password != "" {
108+
return decryptPassword(response.Password, privateKey)
109+
}
110+
return response.Password, err
111+
}
112+
113+
func decryptPassword(encryptedPassword string, privateKey *rsa.PrivateKey) (string, error) {
114+
b64EncryptedPassword := make([]byte, base64.StdEncoding.DecodedLen(len(encryptedPassword)))
115+
116+
n, err := base64.StdEncoding.Decode(b64EncryptedPassword, []byte(encryptedPassword))
117+
if err != nil {
118+
return "", fmt.Errorf("Failed to base64 decode encrypted password: %s", err)
119+
}
120+
password, err := rsa.DecryptPKCS1v15(nil, privateKey, b64EncryptedPassword[0:n])
121+
if err != nil {
122+
return "", fmt.Errorf("Failed to decrypt password: %s", err)
123+
}
124+
125+
return string(password), nil
126+
}
127+
85128
// ExtractImageID gets the ID of the newly created server image from the header
86129
func (res CreateImageResult) ExtractImageID() (string, error) {
87130
if res.Err != nil {

openstack/compute/v2/servers/urls.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,7 @@ func listAddressesURL(client *gophercloud.ServiceClient, id string) string {
4545
func listAddressesByNetworkURL(client *gophercloud.ServiceClient, id, network string) string {
4646
return client.ServiceURL("servers", id, "ips", network)
4747
}
48+
49+
func passwordURL(client *gophercloud.ServiceClient, id string) string {
50+
return client.ServiceURL("servers", id, "os-server-password")
51+
}

openstack/compute/v2/servers/urls_test.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,9 @@ func TestMetadataURL(t *testing.T) {
6666
expected := endpoint + "servers/foo/metadata"
6767
th.CheckEquals(t, expected, actual)
6868
}
69+
70+
func TestPasswordURL(t *testing.T) {
71+
actual := passwordURL(endpointClient(), "foo")
72+
expected := endpoint + "servers/foo/os-server-password"
73+
th.CheckEquals(t, expected, actual)
74+
}

0 commit comments

Comments
 (0)