From 6ff49f2212713d72b55e8304d0da732c4c7a4f9d Mon Sep 17 00:00:00 2001 From: zaidusmani26 <106174728+zaidusmani26@users.noreply.github.com> Date: Mon, 30 Oct 2023 15:49:36 -0700 Subject: [PATCH] feat(amt): adds execute support to remoteaccess (#106) Co-authored-by: Mike --- pkg/amt/remoteaccess/policyappliestomps.go | 118 ++++++++++--- .../remoteaccess/policyappliestomps_test.go | 167 +++++++++++++----- pkg/amt/remoteaccess/policyrule.go | 113 ++++++++++-- pkg/amt/remoteaccess/policyrule_test.go | 114 +++++++++--- pkg/amt/remoteaccess/service.go | 97 ++++++++-- pkg/amt/remoteaccess/service_test.go | 152 +++++++++++----- .../remoteaccess/policyappliestomps/get.xml | 47 +++++ .../amt/remoteaccess/policyrule/enumerate.xml | 22 +++ .../amt/remoteaccess/policyrule/get.xml | 29 +++ .../amt/remoteaccess/service/enumerate.xml | 22 +++ .../amt/remoteaccess/service/get.xml | 27 +++ 11 files changed, 746 insertions(+), 162 deletions(-) create mode 100644 pkg/wsmantesting/responses/amt/remoteaccess/policyappliestomps/get.xml create mode 100644 pkg/wsmantesting/responses/amt/remoteaccess/policyrule/enumerate.xml create mode 100644 pkg/wsmantesting/responses/amt/remoteaccess/policyrule/get.xml create mode 100644 pkg/wsmantesting/responses/amt/remoteaccess/service/enumerate.xml create mode 100644 pkg/wsmantesting/responses/amt/remoteaccess/service/get.xml diff --git a/pkg/amt/remoteaccess/policyappliestomps.go b/pkg/amt/remoteaccess/policyappliestomps.go index cde2fb0c..3a628a94 100644 --- a/pkg/amt/remoteaccess/policyappliestomps.go +++ b/pkg/amt/remoteaccess/policyappliestomps.go @@ -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"` @@ -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 { @@ -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) +// } diff --git a/pkg/amt/remoteaccess/policyappliestomps_test.go b/pkg/amt/remoteaccess/policyappliestomps_test.go index b3c6c3c4..98112690 100644 --- a/pkg/amt/remoteaccess/policyappliestomps_test.go +++ b/pkg/amt/remoteaccess/policyappliestomps_test.go @@ -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 = `http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous0` + GetBodyApply = `AMT_AuthorizationServiceIntel(r) AMT Authorization ServiceIntel(r) AMT Alarm Clock ServiceCIM_ComputerSystemManagedSystem` +) + +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, `testtesttesttesttest1test1testtesttest02`, "", 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, `testtesttesttesttest1test1testtesttest02`, "", 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, "", "Instance", 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, "", "Instance", 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) }) } }) diff --git a/pkg/amt/remoteaccess/policyrule.go b/pkg/amt/remoteaccess/policyrule.go index 0336c758..ac55d995 100644 --- a/pkg/amt/remoteaccess/policyrule.go +++ b/pkg/amt/remoteaccess/policyrule.go @@ -6,11 +6,41 @@ 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/common" + "github.com/open-amt-cloud-toolkit/go-wsman-messages/pkg/wsman" ) const AMT_RemoteAccessPolicyRule = "AMT_RemoteAccessPolicyRule" +type ( + ResponseRule struct { + *wsman.Message + XMLName xml.Name `xml:"Envelope"` + Header message.Header `xml:"Header"` + BodyRule BodyRule `xml:"Body"` + } + BodyRule struct { + XMLName xml.Name `xml:"Body"` + RemotePolicyRule RemotePolicyRule `xml:"AMT_RemoteAccessPolicyRule"` + + EnumerateResponse common.EnumerateResponse + } + RemotePolicyRule struct { + CreationClassName string + ElementName string + ExtendedData string + PolicyRuleName string + SystemCreationClassName string + SystemName string + Trigger int + TunnelLifeTime int + } +) + type RemoteAccessPolicyRule struct { Trigger Trigger TunnelLifeTime int @@ -27,7 +57,23 @@ const ( ) type PolicyRule struct { - base message.Base + base message.Base + clientPolicy wsman.WSManClient +} + +func (w *ResponseRule) JSONRule() string { + jsonOutput, err := json.Marshal(w.BodyRule) + if err != nil { + return "" + } + return string(jsonOutput) +} + +func NewPolicyRuleWithClient(wsmanMessageCreator *message.WSManMessageCreator, clientPolicy wsman.WSManClient) PolicyRule { + return PolicyRule{ + base: message.NewBaseWithClient(wsmanMessageCreator, AMT_RemoteAccessPolicyRule, clientPolicy), + clientPolicy: clientPolicy, + } } func NewRemoteAccessPolicyRule(wsmanMessageCreator *message.WSManMessageCreator) PolicyRule { @@ -37,27 +83,60 @@ func NewRemoteAccessPolicyRule(wsmanMessageCreator *message.WSManMessageCreator) } // Get retrieves the representation of the instance -func (RemoteAccessPolicyRule PolicyRule) Get() string { - return RemoteAccessPolicyRule.base.Get(nil) +func (RemoteAccessPolicyRule PolicyRule) Get() (response ResponseRule, err error) { + response = ResponseRule{ + Message: &wsman.Message{ + XMLInput: RemoteAccessPolicyRule.base.Get(nil), + }, + } + // send the message to AMT + err = RemoteAccessPolicyRule.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 (RemoteAccessPolicyRule PolicyRule) Enumerate() string { - return RemoteAccessPolicyRule.base.Enumerate() +func (RemoteAccessPolicyRule PolicyRule) Enumerate() (response ResponseRule, err error) { + response = ResponseRule{ + Message: &wsman.Message{ + XMLInput: RemoteAccessPolicyRule.base.Enumerate(), + }, + } + // send the message to AMT + err = RemoteAccessPolicyRule.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 -func (RemoteAccessPolicyRule PolicyRule) Pull(enumerationContext string) string { - return RemoteAccessPolicyRule.base.Pull(enumerationContext) -} +// func (RemoteAccessPolicyRule PolicyRule) Pull(enumerationContext string) string { +// return RemoteAccessPolicyRule.base.Pull(enumerationContext) +// } -// Put will change properties of the selected instance -func (RemoteAccessPolicyRule PolicyRule) Put(remoteAccessPolicyRule RemoteAccessPolicyRule) string { - return RemoteAccessPolicyRule.base.Put(remoteAccessPolicyRule, false, nil) -} +// // Put will change properties of the selected instance +// func (RemoteAccessPolicyRule PolicyRule) Put(remoteAccessPolicyRule RemoteAccessPolicyRule) string { +// return RemoteAccessPolicyRule.base.Put(remoteAccessPolicyRule, false, nil) +// } -// Delete removes a the specified instance -func (RemoteAccessPolicyRule PolicyRule) Delete(handle string) string { - selector := message.Selector{Name: "PolicyRuleName", Value: handle} - return RemoteAccessPolicyRule.base.Delete(selector) -} +// // Delete removes a the specified instance +// func (RemoteAccessPolicyRule PolicyRule) Delete(handle string) string { +// selector := message.Selector{Name: "PolicyRuleName", Value: handle} +// return RemoteAccessPolicyRule.base.Delete(selector) +// } diff --git a/pkg/amt/remoteaccess/policyrule_test.go b/pkg/amt/remoteaccess/policyrule_test.go index aa4017bd..e465d0b3 100644 --- a/pkg/amt/remoteaccess/policyrule_test.go +++ b/pkg/amt/remoteaccess/policyrule_test.go @@ -6,49 +6,123 @@ package remoteaccess import ( - "testing" - + "encoding/xml" + "fmt" "github.com/stretchr/testify/assert" + "io" + "os" + "strings" + "testing" "github.com/open-amt-cloud-toolkit/go-wsman-messages/internal/message" + "github.com/open-amt-cloud-toolkit/go-wsman-messages/pkg/common" "github.com/open-amt-cloud-toolkit/go-wsman-messages/pkg/wsmantesting" ) +type MockClientPolicy struct { +} + +const ( + EnvelopeResponsePolicy = `http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous0` + GetBodyPolicy = `AMT_RemoteAccessPolicyRuleIntel(r) AMT Remote Access Policy RuleIntel(r) AMT Remote Access Policy RuleCIM_ComputerSystemManagedSystem` +) + +func (c *MockClientPolicy) Post(msg string) ([]byte, error) { + // read an xml file from disk: + xmlFile, err := os.Open("../../wsmantesting/responses/amt/remoteaccess/policyrule/" + 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_RemoteAccessPolicyRule(t *testing.T) { messageID := 0 resourceUriBase := "http://intel.com/wbem/wscim/1/amt-schema/1/" wsmanMessageCreator := message.NewWSManMessageCreator(resourceUriBase) - elementUnderTest := NewRemoteAccessPolicyRule(wsmanMessageCreator) + clientPolicy := MockClientPolicy{} + elementUnderTest := NewPolicyRuleWithClient(wsmanMessageCreator, &clientPolicy) 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() (ResponseRule, error) + expectedResponse interface{} }{ //GETS - {"should create a valid AMT_RemoteAccessPolicyRule Get wsman message", "AMT_RemoteAccessPolicyRule", wsmantesting.GET, "", "", elementUnderTest.Get}, + { + "should create a valid AMT_RemoteAccessPolicyRule Get wsman message", + "AMT_RemoteAccessPolicyRule", + wsmantesting.GET, + "", + "", + func() (ResponseRule, error) { + currentMessage = "Get" + return elementUnderTest.Get() + }, + BodyRule{ + XMLName: xml.Name{Space: "http://www.w3.org/2003/05/soap-envelope", Local: "Body"}, + RemotePolicyRule: RemotePolicyRule{ + CreationClassName: "AMT_RemoteAccessPolicyRule", + ElementName: "Inte(r) AMT:Remote Access Policy", + ExtendedData: "AAAAAAAAABk=", + PolicyRuleName: "Periodic", + SystemCreationClassName: "CIM_ComputerSystem", + SystemName: "Intel(r) AMT", + Trigger: 2, + TunnelLifeTime: 0, + }, + }, + }, //ENUMERATES - {"should create a valid AMT_RemoteAccessPolicyRule Enumerate wsman message", "AMT_RemoteAccessPolicyRule", wsmantesting.ENUMERATE, wsmantesting.ENUMERATE_BODY, "", elementUnderTest.Enumerate}, + { + "should create a valid AMT_RemoteAccessPolicyRule Enumerate wsman message", + "AMT_RemoteAccessPolicyRule", + wsmantesting.ENUMERATE, + wsmantesting.ENUMERATE_BODY, + "", + func() (ResponseRule, error) { + currentMessage = "Enumerate" + return elementUnderTest.Enumerate() + }, + BodyRule{ + XMLName: xml.Name{Space: "http://www.w3.org/2003/05/soap-envelope", Local: "Body"}, + EnumerateResponse: common.EnumerateResponse{ + EnumerationContext: "D3000000-0000-0000-0000-000000000000", + }, + }, + }, //PULLS - {"should create a valid AMT_RemoteAccessPolicyRule Pull wsman message", "AMT_RemoteAccessPolicyRule", wsmantesting.PULL, wsmantesting.PULL_BODY, "", func() string { return elementUnderTest.Pull(wsmantesting.EnumerationContext) }}, + //{"should create a valid AMT_RemoteAccessPolicyRule Pull wsman message", "AMT_RemoteAccessPolicyRule", wsmantesting.PULL, wsmantesting.PULL_BODY, "", func() string { return elementUnderTest.Pull(wsmantesting.EnumerationContext) }}, //DELETE - {"should create a valid AMT_RemoteAccessPolicyRule Delete wsman message", "AMT_RemoteAccessPolicyRule", wsmantesting.DELETE, "", "Instance", func() string { - return elementUnderTest.Delete("Instance") - }}, + //{"should create a valid AMT_RemoteAccessPolicyRule Delete wsman message", "AMT_RemoteAccessPolicyRule", wsmantesting.DELETE, "", "Instance", 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.BodyRule) }) } }) diff --git a/pkg/amt/remoteaccess/service.go b/pkg/amt/remoteaccess/service.go index 563422b4..196afc47 100644 --- a/pkg/amt/remoteaccess/service.go +++ b/pkg/amt/remoteaccess/service.go @@ -6,16 +6,40 @@ package remoteaccess 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/common" + "github.com/open-amt-cloud-toolkit/go-wsman-messages/pkg/wsman" ) const AMT_RemoteAccessService = "AMT_RemoteAccessService" +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"` + RemoteAccess RemoteAccess `xml:"AMT_RemoteAccessService"` + + EnumerateResponse common.EnumerateResponse + } + RemoteAccess struct { + CreationClassName string + ElementName string + Name string + SystemCreationClassName string + SystemName string + } +) type Service struct { - base message.Base + base message.Base + client wsman.WSManClient } type MPServer struct { AccessInfo string @@ -42,27 +66,76 @@ const ( UsernamePasswordAuthentication MPServerAuthMethod = 2 ) +func (w *Response) JSONStomps() string { + jsonOutput, err := json.Marshal(w.Body) + if err != nil { + return "" + } + return string(jsonOutput) +} + func NewRemoteAccessService(wsmanMessageCreator *message.WSManMessageCreator) Service { return Service{ base: message.NewBase(wsmanMessageCreator, AMT_RemoteAccessService), } } +func NewRemoteAccessServiceWithClient(wsmanMessageCreator *message.WSManMessageCreator, client wsman.WSManClient) Service { + return Service{ + base: message.NewBaseWithClient(wsmanMessageCreator, AMT_RemoteAccessService, client), + client: client, + } +} + // Get retrieves the representation of the instance -func (RemoteAccessService Service) Get() string { - return RemoteAccessService.base.Get(nil) +func (RemoteAccessService Service) Get() (response Response, err error) { + response = Response{ + Message: &wsman.Message{ + XMLInput: RemoteAccessService.base.Get(nil), + }, + } + // send the message to AMT + err = RemoteAccessService.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 (RemoteAccessService Service) Enumerate() string { - return RemoteAccessService.base.Enumerate() +func (RemoteAccessService Service) Enumerate() (response Response, err error) { + response = Response{ + Message: &wsman.Message{ + XMLInput: RemoteAccessService.base.Enumerate(), + }, + } + // send the message to AMT + err = RemoteAccessService.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 -func (RemoteAccessService Service) Pull(enumerationContext string) string { - return RemoteAccessService.base.Pull(enumerationContext) -} -func (r Service) AddMPS(mpServer MPServer) string { +// func (RemoteAccessService Service) Pull(enumerationContext string) string { +// return RemoteAccessService.base.Pull(enumerationContext) +// } + +/*func (r Service) AddMPS(mpServer MPServer) string { header := r.base.WSManMessageCreator.CreateHeader(string(actions.AddMps), AMT_RemoteAccessService, nil, "", "") body := fmt.Sprintf(`%s%d%d%d%s%s%s`, r.base.WSManMessageCreator.ResourceURIBase, AMT_RemoteAccessService, mpServer.AccessInfo, mpServer.InfoFormat, mpServer.Port, mpServer.AuthMethod, mpServer.Username, mpServer.Password, mpServer.CommonName) return r.base.WSManMessageCreator.CreateXML(header, body) @@ -78,4 +151,4 @@ func (r Service) AddRemoteAccessPolicyRule(remoteAccessPolicyRule RemoteAccessPo r.base.WSManMessageCreator.ResourceURIBase, "AMT_ManagementPresenceRemoteSAP", selector.Name, selector.Value) return r.base.WSManMessageCreator.CreateXML(header, body) -} +}*/ diff --git a/pkg/amt/remoteaccess/service_test.go b/pkg/amt/remoteaccess/service_test.go index 4c50f70e..43dd8ee6 100644 --- a/pkg/amt/remoteaccess/service_test.go +++ b/pkg/amt/remoteaccess/service_test.go @@ -6,70 +6,140 @@ 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/amt/actions" + "github.com/open-amt-cloud-toolkit/go-wsman-messages/pkg/common" "github.com/open-amt-cloud-toolkit/go-wsman-messages/pkg/wsmantesting" ) -func TestAMT_AlarmClockService(t *testing.T) { +type MockClient struct { +} + +const ( + EnvelopeResponse = `http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous0` + GetBody = `AMT_RemoteAccessServiceIntel(r) AMT Remote Access ServiceIntel(r) AMT Remote Access ServiceCIM_ComputerSystemManagedSystem` +) + +var currentMessage = "" + +func (c *MockClient) Post(msg string) ([]byte, error) { + // read an xml file from disk: + xmlFile, err := os.Open("../../wsmantesting/responses/amt/remoteaccess/service/" + 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_RemoteAccessService(t *testing.T) { messageID := 0 resourceUriBase := "http://intel.com/wbem/wscim/1/amt-schema/1/" wsmanMessageCreator := message.NewWSManMessageCreator(resourceUriBase) - elementUnderTest := NewRemoteAccessService(wsmanMessageCreator) - + client := MockClient{} + elementUnderTest := NewRemoteAccessServiceWithClient(wsmanMessageCreator, &client) t.Run("amt_* Tests", func(t *testing.T) { tests := []struct { - name string - method string - action string - body string - responseFunc func() string + name string + method string + action string + body string + responseFunc func() (Response, error) + expectedResponse interface{} }{ //GETS - {"should create a valid AMT_RemoteAccessService Get wsman message", "AMT_RemoteAccessService", wsmantesting.GET, "", elementUnderTest.Get}, + { + "should create a valid AMT_RemoteAccessService Get wsman message", + "AMT_RemoteAccessService", + wsmantesting.GET, + "", + func() (Response, error) { + currentMessage = "Get" + return elementUnderTest.Get() + }, + Body{ + XMLName: xml.Name{Space: "http://www.w3.org/2003/05/soap-envelope", Local: "Body"}, + RemoteAccess: RemoteAccess{ + CreationClassName: "AMT_RemoteAccessService", + ElementName: "Intel(r) AMT Remote Access Service", + Name: "Intel(r) AMT Remote Access Service", + SystemCreationClassName: "CIM_ComputerSystem", + SystemName: "Intel(r) AMT", + }, + }, + }, //ENUMERATES - {"should create a valid AMT_RemoteAccessService Enumerate wsman message", "AMT_RemoteAccessService", wsmantesting.ENUMERATE, wsmantesting.ENUMERATE_BODY, elementUnderTest.Enumerate}, + { + "should create a valid AMT_RemoteAccessService Enumerate wsman message", + "AMT_RemoteAccessService", + wsmantesting.ENUMERATE, + wsmantesting.ENUMERATE_BODY, + func() (Response, error) { + currentMessage = "Enumerate" + return elementUnderTest.Enumerate() + }, + Body{ + XMLName: xml.Name{Space: "http://www.w3.org/2003/05/soap-envelope", Local: "Body"}, + EnumerateResponse: common.EnumerateResponse{ + EnumerationContext: "D3000000-0000-0000-0000-000000000000", + }, + }, + }, //PULLS - {"should create a valid AMT_RemoteAccessService Pull wsman message", "AMT_RemoteAccessService", wsmantesting.PULL, wsmantesting.PULL_BODY, func() string { return elementUnderTest.Pull(wsmantesting.EnumerationContext) }}, - {"should create a valid AMT_RemoteAccessService AddMPS wsman message", "AMT_RemoteAccessService", string(actions.AddMps), fmt.Sprintf(`%s%d%d%d%s%s%s`, resourceUriBase, AMT_RemoteAccessService, "AccessInfo", 1, 2, 3, "Username", "Password", "CommonName"), func() string { - mpsServer := MPServer{ - AccessInfo: "AccessInfo", - InfoFormat: 1, - Port: 2, - AuthMethod: 3, - Username: "Username", - Password: "Password", - CommonName: "CommonName", - } - return elementUnderTest.AddMPS(mpsServer) - }}, - {"should create a valid AMT_RemoteAccessPolicyRule wsman message", "AMT_RemoteAccessService", string(actions.AddRemoteAccessPolicyRule), fmt.Sprintf(`%d%d%s
http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous
%s%strue
`, resourceUriBase, AMT_RemoteAccessService, 2, 0, "0300", "http://intel.com/wbem/wscim/1/amt-schema/1/", "AMT_ManagementPresenceRemoteSAP"), func() string { - remoteAccessPolicyRule := RemoteAccessPolicyRule{ - Trigger: 2, - TunnelLifeTime: 0, - ExtendedData: "0300", - } - selector := message.Selector{ - Name: "myselector", - Value: "true", - } - return elementUnderTest.AddRemoteAccessPolicyRule(remoteAccessPolicyRule, selector) - }}, + // {"should create a valid AMT_RemoteAccessService Pull wsman message", "AMT_RemoteAccessService", wsmantesting.PULL, wsmantesting.PULL_BODY, func() string { return elementUnderTest.Pull(wsmantesting.EnumerationContext) }}, + // {"should create a valid AMT_RemoteAccessService AddMPS wsman message", "AMT_RemoteAccessService", string(actions.AddMps), fmt.Sprintf(`%s%d%d%d%s%s%s`, resourceUriBase, AMT_RemoteAccessService, "AccessInfo", 1, 2, 3, "Username", "Password", "CommonName"), func() string { + // mpsServer := MPServer{ + // AccessInfo: "AccessInfo", + // InfoFormat: 1, + // Port: 2, + // AuthMethod: 3, + // Username: "Username", + // Password: "Password", + // CommonName: "CommonName", + // } + // return elementUnderTest.AddMPS(mpsServer) + // }}, + // {"should create a valid AMT_RemoteAccessPolicyRule wsman message", "AMT_RemoteAccessService", string(actions.AddRemoteAccessPolicyRule), fmt.Sprintf(`%d%d%s
http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous
%s%strue
`, resourceUriBase, AMT_RemoteAccessService, 2, 0, "0300", "http://intel.com/wbem/wscim/1/amt-schema/1/", "AMT_ManagementPresenceRemoteSAP"), func() string { + // remoteAccessPolicyRule := RemoteAccessPolicyRule{ + // Trigger: 2, + // TunnelLifeTime: 0, + // ExtendedData: "0300", + // } + // selector := message.Selector{ + // Name: "myselector", + // Value: "true", + // } + // return elementUnderTest.AddRemoteAccessPolicyRule(remoteAccessPolicyRule, selector) + // }}, } for _, test := range tests { t.Run(test.name, func(t *testing.T) { - correctResponse := wsmantesting.ExpectedResponse(messageID, resourceUriBase, test.method, test.action, "", 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.Body) }) } }) diff --git a/pkg/wsmantesting/responses/amt/remoteaccess/policyappliestomps/get.xml b/pkg/wsmantesting/responses/amt/remoteaccess/policyappliestomps/get.xml new file mode 100644 index 00000000..c34d3e43 --- /dev/null +++ b/pkg/wsmantesting/responses/amt/remoteaccess/policyappliestomps/get.xml @@ -0,0 +1,47 @@ + + + + http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous + 0 + http://schemas.xmlsoap.org/ws/2004/09/transfer/GetResponse + uuid:00000000-8086-8086-8086-0000000003A7 + http://intel.com/wbem/wscim/1/amt-schema/1/AMT_RemoteAccessPolicyAppliesToMPS + + + + + http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous + + http://intel.com/wbem/wscim/1/amt-schema/1/AMT_ManagementPresenceRemoteSAP + + AMT_ManagementPresenceRemoteSAP + Intel(r) AMT:Management Presence Server 0 + CIM_ComputerSystem + Intel(r) AMT + + + + 2 + 0 + + http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous + + http://intel.com/wbem/wscim/1/amt-schema/1/AMT_RemoteAccessPolicyRule + + AMT_RemoteAccessPolicyRule + Periodic + CIM_ComputerSystem + Intel(r) AMT + + + + + + \ No newline at end of file diff --git a/pkg/wsmantesting/responses/amt/remoteaccess/policyrule/enumerate.xml b/pkg/wsmantesting/responses/amt/remoteaccess/policyrule/enumerate.xml new file mode 100644 index 00000000..3bbb20f2 --- /dev/null +++ b/pkg/wsmantesting/responses/amt/remoteaccess/policyrule/enumerate.xml @@ -0,0 +1,22 @@ + + + + http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous + 0 + http://schemas.xmlsoap.org/ws/2004/09/enumeration/EnumerateResponse + uuid:00000000-8086-8086-8086-000000000322 + http://intel.com/wbem/wscim/1/amt-schema/1/AMT_RemoteAccessPolicyRule + + + + D3000000-0000-0000-0000-000000000000 + + + \ No newline at end of file diff --git a/pkg/wsmantesting/responses/amt/remoteaccess/policyrule/get.xml b/pkg/wsmantesting/responses/amt/remoteaccess/policyrule/get.xml new file mode 100644 index 00000000..26105f9b --- /dev/null +++ b/pkg/wsmantesting/responses/amt/remoteaccess/policyrule/get.xml @@ -0,0 +1,29 @@ + + + + http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous + 0 + http://schemas.xmlsoap.org/ws/2004/09/transfer/GetResponse + uuid:00000000-8086-8086-8086-0000000003B7 + http://intel.com/wbem/wscim/1/amt-schema/1/AMT_RemoteAccessPolicyRule + + + + AMT_RemoteAccessPolicyRule + Inte(r) AMT:Remote Access Policy + AAAAAAAAABk= + Periodic + CIM_ComputerSystem + Intel(r) AMT + 2 + 0 + + + diff --git a/pkg/wsmantesting/responses/amt/remoteaccess/service/enumerate.xml b/pkg/wsmantesting/responses/amt/remoteaccess/service/enumerate.xml new file mode 100644 index 00000000..8e000fa0 --- /dev/null +++ b/pkg/wsmantesting/responses/amt/remoteaccess/service/enumerate.xml @@ -0,0 +1,22 @@ + + + + http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous + 0 + http://schemas.xmlsoap.org/ws/2004/09/enumeration/EnumerateResponse + uuid:00000000-8086-8086-8086-000000000322 + http://intel.com/wbem/wscim/1/amt-schema/1/AMT_RemoteAccessService + + + + D3000000-0000-0000-0000-000000000000 + + + \ No newline at end of file diff --git a/pkg/wsmantesting/responses/amt/remoteaccess/service/get.xml b/pkg/wsmantesting/responses/amt/remoteaccess/service/get.xml new file mode 100644 index 00000000..80d83f21 --- /dev/null +++ b/pkg/wsmantesting/responses/amt/remoteaccess/service/get.xml @@ -0,0 +1,27 @@ + + + + http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous + 0 + http://schemas.xmlsoap.org/ws/2004/09/transfer/GetResponse + uuid:00000000-8086-8086-8086-0000000003BC + http://intel.com/wbem/wscim/1/amt-schema/1/AMT_RemoteAccessService + + + + AMT_RemoteAccessService + Intel(r) AMT Remote Access Service + Intel(r) AMT Remote Access Service + CIM_ComputerSystem + Intel(r) AMT + + + \ No newline at end of file