Skip to content

Commit

Permalink
feat(amt): adds execute support for tls
Browse files Browse the repository at this point in the history
  • Loading branch information
zaidusmani26 committed Oct 16, 2023
1 parent 5ec768a commit ea3a1c1
Show file tree
Hide file tree
Showing 6 changed files with 383 additions and 38 deletions.
87 changes: 82 additions & 5 deletions pkg/amt/tls/credentialcontext.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,55 @@ package tls
import (
"fmt"

"encoding/json"
"encoding/xml"

"github.com/open-amt-cloud-toolkit/go-wsman-messages/internal/message"
//"github.com/open-amt-cloud-toolkit/go-wsman-messages/pkg/amt/actions"
//"github.com/open-amt-cloud-toolkit/go-wsman-messages/pkg/amt/models"
"github.com/open-amt-cloud-toolkit/go-wsman-messages/pkg/common"
"github.com/open-amt-cloud-toolkit/go-wsman-messages/pkg/wsman"
)

type (
ResponseCredentials struct {
*wsman.Message
XMLName xml.Name `xml:"Envelope"`
Header message.Header `xml:"Header"`
BodyCredentials BodyCredentials `xml:"BodyCredentials"`
}
BodyCredentials struct {
XMLName xml.Name `xml:"Envelope"`
TlsCredentials TlsCredentials `xml:"AMT_TLSCredentialContext"`

EnumerateResponse common.EnumerateResponse
}
TlsCredentials struct {

}
)
const AMT_TLSCredentialContext = "AMT_TLSCredentialContext"



type CredentialContext struct {
base message.Base
client wsman.WSManClient
}

func (w *ResponseCredentials) JSON() string {
jsonOutput, err := json.Marshal(w.BodyCredentials)
if err != nil {
return ""
}
return string(jsonOutput)
}

func NewTLSCredentialContextWithClient(wsmanMessageCreator *message.WSManMessageCreator, client wsman.WSManClient) CredentialContext {
return CredentialContext{
base: message.NewBaseWithClient(wsmanMessageCreator, AMT_TLSCredentialContext, client),
client: client,
}
}

func NewTLSCredentialContext(wsmanMessageCreator *message.WSManMessageCreator) CredentialContext {
Expand All @@ -24,13 +66,48 @@ func NewTLSCredentialContext(wsmanMessageCreator *message.WSManMessageCreator) C
}

// Get retrieves the representation of the instance
func (TLSCredentialContext CredentialContext) Get() string {
return TLSCredentialContext.base.Get(nil)
}
func (TLSCredentialContext CredentialContext) Get() (response ResponseCredentials, err error) {

response = ResponseCredentials{
Message: &wsman.Message{
XMLInput: TLSCredentialContext.base.Get(nil),
},
}

// send the message to AMT
err = TLSCredentialContext.base.Execute(response.Message)
if err != nil {
return
}

// put the xml response into the go struct
err = xml.Unmarshal([]byte(response.XMLOutput), &response)
if err != nil {
return
}

return
}
// Enumerates the instances of this class
func (TLSCredentialContext CredentialContext) Enumerate() string {
return TLSCredentialContext.base.Enumerate()
func (TLSCredentialContext CredentialContext) Enumerate() (response ResponseCredentials, err error) {
response = ResponseCredentials{
Message: &wsman.Message{
XMLInput: TLSCredentialContext.base.Enumerate(),
},
}
// send the message to AMT
err = TLSCredentialContext.base.Execute(response.Message)
if err != nil {
return
}

// put the xml response into the go struct
err = xml.Unmarshal([]byte(response.XMLOutput), &response)
if err != nil {
return
}

return
}

// Pulls instances of this class, following an Enumerate operation
Expand Down
96 changes: 84 additions & 12 deletions pkg/amt/tls/credentialcontext_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,59 @@
package tls

import (
"encoding/xml"
"fmt"
"io"
"os"
"strings"
"testing"

"github.com/stretchr/testify/assert"

"github.com/open-amt-cloud-toolkit/go-wsman-messages/pkg/common"
"github.com/open-amt-cloud-toolkit/go-wsman-messages/internal/message"
"github.com/open-amt-cloud-toolkit/go-wsman-messages/pkg/wsman"
"github.com/open-amt-cloud-toolkit/go-wsman-messages/pkg/wsmantesting"
)

type MockClientCredentials struct {
}

const (
EnvelopeResponseCredentials = `<a:Envelope xmlns:a="http://www.w3.org/2003/05/soap-envelope" x-mlns:b="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:c="http://schemas.dmtf.org/wbem/wsman/1/wsman.xsd" xmlns:d="http://schemas.xmlsoap.org/ws/2005/02/trust" xmlns:e="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:f="http://schemas.dmtf.org/wbem/wsman/1/cimbinding.xsd" xmlns:g="http://intel.com/wbem/wscim/1/amt-schema/1/AMT_TLSCredentialContext" xmlns:h="http://schemas.dmtf.org/wbem/wscim/1/common" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><a:Header><b:To>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</b:To><b:RelatesTo>0</b:RelatesTo><b:Action a:mustUnderstand="true">`
GetBodyCredentials = `<g:AMT_TLSCredentialContext><g:CreationClassName>AMT_TLSCredentialContext</g:CreationClassName><g:ElementName>Intel(r) AMT TLS Credential Context</g:ElementName><g:Name>Intel(r) AMT TLS Credential Context</g:Name><g:SystemCreationClassName>CIM_ComputerSystem</g:SystemCreationClassName><g:SystemName>ManagedSystem</g:SystemName></g:AMT_TLSCredentialCOntext>`
)

var currentMessageCredentials = ""

func (c *MockClient) PostCredentials(msg string) ([]byte, error) {
// read an xml file from disk:
xmlFile, err := os.Open("../../wsmantesting/responses/amt/tls" + strings.ToLower(currentMessageCredentials) + ".xml")
if err != nil {
fmt.Println("Error opening file:", err)
return nil, err
}
defer xmlFile.Close()
// read file into string
xmlData, err := io.ReadAll(xmlFile)
if err != nil {
fmt.Println("Error reading file:", err)
return nil, err
}
// strip carriage returns and new line characters
xmlData = []byte(strings.ReplaceAll(string(xmlData), "\r\n", ""))

// Simulate a successful response for testing.
return []byte(xmlData), nil
}
func TestAMT_TLSCredentialContext(t *testing.T) {
messageID := 0
resourceUriBase := "http://intel.com/wbem/wscim/1/amt-schema/1/"
wsmanMessageCreator := message.NewWSManMessageCreator(resourceUriBase)
elementUnderTest := NewTLSCredentialContext(wsmanMessageCreator)
//client := MockClient{} // wsman.NewClient("http://localhost:16992/wsman", "admin", "P@ssw0rd", true)
//elementUnderTest := NewServiceWithClient(wsmanMessageCreator, &client)
// enumerationId := ""
client := wsman.NewClient("http://localhost:16992/wsman", "admin", "Intel123!", true)
elementUnderTest := NewTLSCredentialContextWithClient(wsmanMessageCreator, client)

t.Run("amt_* Tests", func(t *testing.T) {
tests := []struct {
Expand All @@ -27,26 +67,58 @@ func TestAMT_TLSCredentialContext(t *testing.T) {
action string
body string
extraHeader string
responseFunc func() string
responseFunc func() (ResponseCredentials, error)
expectedResponse interface{}
}{
//GETS
{"should create a valid AMT_TLSCredentialContext Get wsman message", "AMT_TLSCredentialContext", wsmantesting.GET, "", "", elementUnderTest.Get},
{"should create a valid AMT_TLSCredentialContext Get wsman message",
"AMT_TLSCredentialContext",
wsmantesting.GET,
"",
"",
func() (ResponseCredentials, error) {
//currentMessage = "Get"
return elementUnderTest.Get()
},
BodyCredentials{
XMLName: xml.Name{Space: "http://www.w3.org/2003/05/soap-envelope", Local: "Body"},
TlsCredentials: TlsCredentials{

},
},
},
//ENUMERATES
{"should create a valid AMT_TLSCredentialContext Enumerate wsman message", "AMT_TLSCredentialContext", wsmantesting.ENUMERATE, wsmantesting.ENUMERATE_BODY, "", elementUnderTest.Enumerate},
{"should create a valid AMT_TLSCredentialContext Enumerate wsman message",
"AMT_TLSCredentialContext",
wsmantesting.ENUMERATE,
wsmantesting.ENUMERATE_BODY,
"",
func() (ResponseCredentials, error) {
//client.CurrentMessage = "Enumerate"
return elementUnderTest.Enumerate()
},
BodyCredentials{
XMLName: xml.Name{Space: "http://www.w3.org/2003/05/soap-envelope", Local: "Body"},
EnumerateResponse: common.EnumerateResponse{
EnumerationContext: "5C000000-0000-0000-0000-000000000000",
},
},
},
//PULLS
{"should create a valid AMT_TLSCredentialContext Pull wsman message", "AMT_TLSCredentialContext", wsmantesting.PULL, wsmantesting.PULL_BODY, "", func() string { return elementUnderTest.Pull(wsmantesting.EnumerationContext) }},
//{"should create a valid AMT_TLSCredentialContext Pull wsman message", "AMT_TLSCredentialContext", wsmantesting.PULL, wsmantesting.PULL_BODY, "", func() string { return elementUnderTest.Pull(wsmantesting.EnumerationContext) }},
//DELETE
{"should create a valid AMT_TLSCredentialContext Delete wsman message", "AMT_TLSCredentialContext", wsmantesting.DELETE, "", "<w:SelectorSet><w:Selector Name=\"Name\">instanceID123</w:Selector></w:SelectorSet>", func() string { return elementUnderTest.Delete("instanceID123") }},
//{"should create a valid AMT_TLSCredentialContext Delete wsman message", "AMT_TLSCredentialContext", wsmantesting.DELETE, "", "<w:SelectorSet><w:Selector Name=\"Name\">instanceID123</w:Selector></w:SelectorSet>", func() string { return elementUnderTest.Delete("instanceID123") }},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
correctResponse := wsmantesting.ExpectedResponse(messageID, resourceUriBase, test.method, test.action, test.extraHeader, test.body)
expectedXMLInput := wsmantesting.ExpectedResponse(messageID, resourceUriBase, test.method, test.action, "", test.body)
messageID++
response := test.responseFunc()
if response != correctResponse {
assert.Equal(t, correctResponse, response)
}
response, err := test.responseFunc()
println(response.XMLOutput)
assert.NoError(t, err)
assert.Equal(t, expectedXMLInput, response.XMLInput)
assert.Equal(t, test.expectedResponse, response.BodyCredentials)
})
}
})
Expand Down
85 changes: 80 additions & 5 deletions pkg/amt/tls/settingdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,33 @@
package tls

import (
"encoding/xml"
"encoding/json"

"github.com/open-amt-cloud-toolkit/go-wsman-messages/internal/message"
//"github.com/open-amt-cloud-toolkit/go-wsman-messages/pkg/amt/actions"
"github.com/open-amt-cloud-toolkit/go-wsman-messages/pkg/cim/models"
"github.com/open-amt-cloud-toolkit/go-wsman-messages/pkg/common"
"github.com/open-amt-cloud-toolkit/go-wsman-messages/pkg/wsman"
)

type (
Response struct {
*wsman.Message
XMLName xml.Name `xml:"Envelope"`
Header message.Header `xml:"Header"`
Body Body `xml:"Body"`
}
Body struct {
XMLName xml.Name `xml:"Body"`
TlsSetting TlsSetting `xml:"AMT_TLSSettingData"`

EnumerateResponse common.EnumerateResponse
}
TlsSetting struct{

}
)
const AMT_TLSSettingData = "AMT_TLSSettingData"

type TLSSettingData struct {
Expand All @@ -20,24 +43,76 @@ type TLSSettingData struct {
AcceptNonSecureConnections bool
NonSecureConnectionsSupported bool
}

func (w *Response) JSON() string {
jsonOutput, err := json.Marshal(w.Body)
if err != nil {
return ""
}
return string(jsonOutput)
}

type SettingData struct {
base message.Base
client wsman.WSManClient
}

func NewTLSSettingDataWithClient(wsmanMessageCreator *message.WSManMessageCreator, client wsman.WSManClient) SettingData {
return SettingData{
base: message.NewBaseWithClient(wsmanMessageCreator, AMT_TLSSettingData, client),
client : client,
}
}

func NewTLSSettingData(wsmanMessageCreator *message.WSManMessageCreator) SettingData {
return SettingData{
base: message.NewBase(wsmanMessageCreator, AMT_TLSSettingData),
}
}

// Get retrieves the representation of the instance
func (TLSSettingData SettingData) Get() string {
return TLSSettingData.base.Get(nil)
func (TLSSettingData SettingData) Get() (response Response, err error) {

response = Response{
Message: &wsman.Message{
XMLInput: TLSSettingData.base.Get(nil),
},
}

// send the message to AMT
err = TLSSettingData.base.Execute(response.Message)
if err != nil {
return
}

// put the xml response into the go struct
err = xml.Unmarshal([]byte(response.XMLOutput), &response)
if err != nil {
return
}

return
}

// Enumerates the instances of this class
func (TLSSettingData SettingData) Enumerate() string {
return TLSSettingData.base.Enumerate()
func (TLSSettingData SettingData) Enumerate() (response Response, err error) {
response = Response{
Message: &wsman.Message{
XMLInput: TLSSettingData.base.Enumerate(),
},
}
// send the message to AMT
err = TLSSettingData.base.Execute(response.Message)
if err != nil {
return
}

// put the xml response into the go struct
err = xml.Unmarshal([]byte(response.XMLOutput), &response)
if err != nil {
return
}

return
}

// Pulls instances of this class, following an Enumerate operation
Expand Down
Loading

0 comments on commit ea3a1c1

Please sign in to comment.