Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(amt): adds execute support to remoteaccess #106

Merged
merged 7 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
118 changes: 95 additions & 23 deletions pkg/amt/remoteaccess/policyappliestomps.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,37 @@
package remoteaccess

import (
"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/cim/models"
"github.com/open-amt-cloud-toolkit/go-wsman-messages/pkg/common"
"github.com/open-amt-cloud-toolkit/go-wsman-messages/pkg/wsman"
)

const AMT_RemoteAccessPolicyAppliesToMPS = "AMT_RemoteAccessPolicyAppliesToMPS"

type (
ResponseApplies struct {
*wsman.Message
XMLName xml.Name `xml:"Envelope"`
Header message.Header `xml:"Header"`
BodyApplies BodyApplies `xml:"Body"`
}
BodyApplies struct {
XMLName xml.Name `xml:"Body"`
PolicyApplies PolicyApplies `xml:"AMT_RemoteAccessPolicyAppliesToMPS"`

EnumerateResponse common.EnumerateResponse
}
PolicyApplies struct {
CreationClassName string
Name string
SystemCreationClassName string
SystemName string
}
)
type RemoteAccessPolicyAppliesToMPS struct {
XMLName xml.Name `xml:"h:AMT_RemoteAccessPolicyAppliesToMPS"`
H string `xml:"xmlns:h,attr"`
Expand Down Expand Up @@ -59,7 +82,23 @@ const (
)

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

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

func NewRemoteAccessPolicyAppliesToMPSWithClient(wsmanMessageCreator *message.WSManMessageCreator, client wsman.WSManClient) PolicyAppliesToMPS {
return PolicyAppliesToMPS{
base: message.NewBaseWithClient(wsmanMessageCreator, AMT_RemoteAccessPolicyAppliesToMPS, client),
client: client,
}
}

func NewRemoteAccessPolicyAppliesToMPS(wsmanMessageCreator *message.WSManMessageCreator) PolicyAppliesToMPS {
Expand All @@ -69,32 +108,65 @@ func NewRemoteAccessPolicyAppliesToMPS(wsmanMessageCreator *message.WSManMessage
}

// Get retrieves the representation of the instance
func (RemoteAccessPolicyAppliesToMPS PolicyAppliesToMPS) Get() string {
return RemoteAccessPolicyAppliesToMPS.base.Get(nil)
}
func (RemoteAccessPolicyAppliesToMPS PolicyAppliesToMPS) Get() (response ResponseApplies, err error) {
response = ResponseApplies{
Message: &wsman.Message{
XMLInput: RemoteAccessPolicyAppliesToMPS.base.Get(nil),
},
}
// send the message to AMT
err = RemoteAccessPolicyAppliesToMPS.base.Execute(response.Message)
if err != nil {
return
}

// Enumerates the instances of this class
func (RemoteAccessPolicyAppliesToMPS PolicyAppliesToMPS) Enumerate() string {
return RemoteAccessPolicyAppliesToMPS.base.Enumerate()
// 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
func (RemoteAccessPolicyAppliesToMPS PolicyAppliesToMPS) Pull(enumerationContext string) string {
return RemoteAccessPolicyAppliesToMPS.base.Pull(enumerationContext)
}
// Enumerates the instances of this class
func (RemoteAccessPolicyAppliesToMPS PolicyAppliesToMPS) Enumerate() (response ResponseApplies, err error) {
response = ResponseApplies{
Message: &wsman.Message{
XMLInput: RemoteAccessPolicyAppliesToMPS.base.Enumerate(),
},
}
// send the message to AMT
err = RemoteAccessPolicyAppliesToMPS.base.Execute(response.Message)
if err != nil {
return
}

// Put will change properties of the selected instance
func (RemoteAccessPolicyAppliesToMPS PolicyAppliesToMPS) Put(remoteAccessPolicyAppliesToMPS *RemoteAccessPolicyAppliesToMPS) string {
return RemoteAccessPolicyAppliesToMPS.base.Put(remoteAccessPolicyAppliesToMPS, false, nil)
}
// put the xml response into the go struct
err = xml.Unmarshal([]byte(response.XMLOutput), &response)
if err != nil {
return
}

// Delete removes a the specified instance
func (RemoteAccessPolicyAppliesToMPS PolicyAppliesToMPS) Delete(handle string) string {
selector := message.Selector{Name: "Name", Value: handle}
return RemoteAccessPolicyAppliesToMPS.base.Delete(selector)
return
}

// Creates a new instance of this class
func (RemoteAccessPolicyAppliesToMPS PolicyAppliesToMPS) Create(remoteAccessPolicyAppliesToMPS RemoteAccessPolicyAppliesToMPS) string {
return RemoteAccessPolicyAppliesToMPS.base.Create(remoteAccessPolicyAppliesToMPS, nil)
}
// Pulls instances of this class, following an Enumerate operation
// func (RemoteAccessPolicyAppliesToMPS PolicyAppliesToMPS) Pull(enumerationContext string) string {
// return RemoteAccessPolicyAppliesToMPS.base.Pull(enumerationContext)
// }

// // Put will change properties of the selected instance
// func (RemoteAccessPolicyAppliesToMPS PolicyAppliesToMPS) Put(remoteAccessPolicyAppliesToMPS *RemoteAccessPolicyAppliesToMPS) string {
// return RemoteAccessPolicyAppliesToMPS.base.Put(remoteAccessPolicyAppliesToMPS, false, nil)
// }

// // Delete removes a the specified instance
// func (RemoteAccessPolicyAppliesToMPS PolicyAppliesToMPS) Delete(handle string) string {
// selector := message.Selector{Name: "Name", Value: handle}
// return RemoteAccessPolicyAppliesToMPS.base.Delete(selector)
// }

// // Creates a new instance of this class
// func (RemoteAccessPolicyAppliesToMPS PolicyAppliesToMPS) Create(remoteAccessPolicyAppliesToMPS RemoteAccessPolicyAppliesToMPS) string {
// return RemoteAccessPolicyAppliesToMPS.base.Create(remoteAccessPolicyAppliesToMPS, nil)
// }
167 changes: 118 additions & 49 deletions pkg/amt/remoteaccess/policyappliestomps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,79 +6,148 @@
package remoteaccess

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

"github.com/stretchr/testify/assert"

"github.com/open-amt-cloud-toolkit/go-wsman-messages/internal/message"
"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/wsmantesting"
)

type MockClientApply struct {
}

const (
EnvelopeResponseApply = `<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_AuthorizationService" 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">`
GetBodyApply = `<g:AMT_AuthorizationService><g:CreationClassName>AMT_AuthorizationService</g:CreationClassName><g:ElementName>Intel(r) AMT Authorization Service</g:ElementName><g:Name>Intel(r) AMT Alarm Clock Service</g:Name><g:SystemCreationClassName>CIM_ComputerSystem</g:SystemCreationClassName><g:SystemName>ManagedSystem</g:SystemName></g:AMT_AuthorizationService>`
)

func (c *MockClientApply) Post(msg string) ([]byte, error) {
// read an xml file from disk:
xmlFile, err := os.Open("../../wsmantesting/responses/amt/remoteaccess/policyappliestomps/" + strings.ToLower(currentMessage) + ".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_RemoteAccessPolicyAppliesToMPS(t *testing.T) {
messageID := 0
resourceUriBase := "http://intel.com/wbem/wscim/1/amt-schema/1/"
wsmanMessageCreator := message.NewWSManMessageCreator(resourceUriBase)
elementUnderTest := NewRemoteAccessPolicyAppliesToMPS(wsmanMessageCreator)

client := MockClientApply{}
elementUnderTest := NewRemoteAccessPolicyAppliesToMPSWithClient(wsmanMessageCreator, &client)
t.Run("amt_* Tests", func(t *testing.T) {
tests := []struct {
name string
method string
action string
body string
extraHeader string
responseFunc func() string
name string
method string
action string
body string
extraHeader string
responseFunc func() (ResponseApplies, error)
expectedResponse interface{}
}{
//GETS
{"should create a valid AMT_RemoteAccessPolicyAppliesToMPS Get wsman message", "AMT_RemoteAccessPolicyAppliesToMPS", wsmantesting.GET, "", "", elementUnderTest.Get},
{
"should create a valid AMT_RemoteAccessPolicyAppliesToMPS Get wsman message",
"AMT_RemoteAccessPolicyAppliesToMPS",
wsmantesting.GET,
"",
"",
func() (ResponseApplies, error) {
currentMessage = "Get"
return elementUnderTest.Get()
},
BodyApplies{
XMLName: xml.Name{Space: "http://www.w3.org/2003/05/soap-envelope", Local: "Body"},
PolicyApplies: PolicyApplies{
CreationClassName: "",
Name: "",
SystemCreationClassName: "",
SystemName: "",
},
},
},
//ENUMERATES
{"should create a valid AMT_RemoteAccessPolicyAppliesToMPS Enumerate wsman message", "AMT_RemoteAccessPolicyAppliesToMPS", wsmantesting.ENUMERATE, wsmantesting.ENUMERATE_BODY, "", elementUnderTest.Enumerate},
//PULLS
{"should create a valid AMT_RemoteAccessPolicyAppliesToMPS Pull wsman message", "AMT_RemoteAccessPolicyAppliesToMPS", wsmantesting.PULL, wsmantesting.PULL_BODY, "", func() string { return elementUnderTest.Pull(wsmantesting.EnumerationContext) }},
{"should create a valid AMT_RemoteAccessPolicyAppliesToMPS Put wsman message", "AMT_RemoteAccessPolicyAppliesToMPS", wsmantesting.PUT, `<h:AMT_RemoteAccessPolicyAppliesToMPS xmlns:h="http://intel.com/wbem/wscim/1/amt-schema/1/AMT_RemoteAccessPolicyAppliesToMPS"><h:PolicySet><h:Caption>test</h:Caption><h:Description>test</h:Description><h:ElementName>test</h:ElementName><h:CommonName>test</h:CommonName><h:PolicyKeywords>test</h:PolicyKeywords><h:PolicyDecisionStrategy>1</h:PolicyDecisionStrategy><h:PolicyRoles>test</h:PolicyRoles><h:Enabled>1</h:Enabled></h:PolicySet><h:ManagedElement><h:Caption>test</h:Caption><h:Description>test</h:Description><h:ElementName>test</h:ElementName></h:ManagedElement><h:OrderOfAccess>0</h:OrderOfAccess><h:MpsType>2</h:MpsType></h:AMT_RemoteAccessPolicyAppliesToMPS>`, "", func() string {
rapatmps := RemoteAccessPolicyAppliesToMPS{
PolicySetAppliesToElement: PolicySetAppliesToElement{
ManagedElement: models.ManagedElement{
Caption: "test",
Description: "test",
ElementName: "test",
},
PolicySet: PolicySet{
Enabled: 1,
PolicyDecisionStrategy: PolicyDecisionStrategyFirstMatching,
PolicyRoles: []string{"test"},
Policy: Policy{
ManagedElement: models.ManagedElement{
Caption: "test",
Description: "test",
ElementName: "test",
},
CommonName: "test",
PolicyKeywords: []string{"test"},
},
},
{
"should create a valid AMT_RemoteAccessPolicyAppliesToMPS Enumerate wsman message",
"AMT_RemoteAccessPolicyAppliesToMPS",
wsmantesting.ENUMERATE,
wsmantesting.ENUMERATE_BODY,
"",
func() (ResponseApplies, error) {
currentMessage = "Enumerate"
return elementUnderTest.Enumerate()
},
BodyApplies{
XMLName: xml.Name{Space: "http://www.w3.org/2003/05/soap-envelope", Local: "Body"},
EnumerateResponse: common.EnumerateResponse{
EnumerationContext: "CE000000-0000-0000-0000-000000000000",
},
MPSType: BothMPS,
OrderOfAccess: 0,
}
},
},
//PULLS
// {"should create a valid AMT_RemoteAccessPolicyAppliesToMPS Pull wsman message", "AMT_RemoteAccessPolicyAppliesToMPS", wsmantesting.PULL, wsmantesting.PULL_BODY, "", func() string { return elementUnderTest.Pull(wsmantesting.EnumerationContext) }},
// {"should create a valid AMT_RemoteAccessPolicyAppliesToMPS Put wsman message", "AMT_RemoteAccessPolicyAppliesToMPS", wsmantesting.PUT, `<h:AMT_RemoteAccessPolicyAppliesToMPS xmlns:h="http://intel.com/wbem/wscim/1/amt-schema/1/AMT_RemoteAccessPolicyAppliesToMPS"><h:PolicySet><h:Caption>test</h:Caption><h:Description>test</h:Description><h:ElementName>test</h:ElementName><h:CommonName>test</h:CommonName><h:PolicyKeywords>test</h:PolicyKeywords><h:PolicyDecisionStrategy>1</h:PolicyDecisionStrategy><h:PolicyRoles>test</h:PolicyRoles><h:Enabled>1</h:Enabled></h:PolicySet><h:ManagedElement><h:Caption>test</h:Caption><h:Description>test</h:Description><h:ElementName>test</h:ElementName></h:ManagedElement><h:OrderOfAccess>0</h:OrderOfAccess><h:MpsType>2</h:MpsType></h:AMT_RemoteAccessPolicyAppliesToMPS>`, "", func() string {
// rapatmps := RemoteAccessPolicyAppliesToMPS{
// PolicySetAppliesToElement: PolicySetAppliesToElement{
// ManagedElement: models.ManagedElement{
// Caption: "test",
// Description: "test",
// ElementName: "test",
// },
// PolicySet: PolicySet{
// Enabled: 1,
// PolicyDecisionStrategy: PolicyDecisionStrategyFirstMatching,
// PolicyRoles: []string{"test"},
// Policy: Policy{
// ManagedElement: models.ManagedElement{
// Caption: "test",
// Description: "test",
// ElementName: "test",
// },
// CommonName: "test",
// PolicyKeywords: []string{"test"},
// },
// },
// },
// MPSType: BothMPS,
// OrderOfAccess: 0,
// }

return elementUnderTest.Put(&rapatmps)
}},
//{"should create a valid AMT_RemoteAccessPolicyAppliesToMPS Create wsman message", "AMT_RemoteAccessPolicyAppliesToMPS", wsmantesting.PULL, wsmantesting.PULL_BODY, "", func() string { return elementUnderTest.Pull(wsmantesting.EnumerationContext) }},
{"should create a valid AMT_RemoteAccessPolicyAppliesToMPS Delete wsman message", "AMT_RemoteAccessPolicyAppliesToMPS", wsmantesting.DELETE, "", "<w:SelectorSet><w:Selector Name=\"Name\">Instance</w:Selector></w:SelectorSet>", func() string {
return elementUnderTest.Delete("Instance")
}},
// return elementUnderTest.Put(&rapatmps)
// }},
// //{"should create a valid AMT_RemoteAccessPolicyAppliesToMPS Create wsman message", "AMT_RemoteAccessPolicyAppliesToMPS", wsmantesting.PULL, wsmantesting.PULL_BODY, "", func() string { return elementUnderTest.Pull(wsmantesting.EnumerationContext) }},
// {"should create a valid AMT_RemoteAccessPolicyAppliesToMPS Delete wsman message", "AMT_RemoteAccessPolicyAppliesToMPS", wsmantesting.DELETE, "", "<w:SelectorSet><w:Selector Name=\"Name\">Instance</w:Selector></w:SelectorSet>", func() string {
// return elementUnderTest.Delete("Instance")
// }},
}

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()
assert.NoError(t, err)
assert.Equal(t, expectedXMLInput, response.XMLInput)
assert.Equal(t, test.expectedResponse, response.BodyApplies)
})
}
})
Expand Down
Loading
Loading