Skip to content

Commit

Permalink
feat: support get certs api
Browse files Browse the repository at this point in the history
Co-authored-by: madhavilosetty-intel <[email protected]>
  • Loading branch information
rjbrache and madhavilosetty-intel committed May 22, 2024
1 parent 0a0052c commit b962771
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 23 deletions.
17 changes: 17 additions & 0 deletions pkg/wsman/amt/publickey/certificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ func (certificate Certificate) Enumerate() (response Response, err error) {

// Pull returns the instances of this class. An enumeration context provided by the Enumerate call is used as input.
func (certificate Certificate) Pull(enumerationContext string) (response Response, err error) {
var refinedOutput []RefinedPublicKeyCertificateResponse
response = Response{
Message: &client.Message{
XMLInput: certificate.base.Pull(enumerationContext),
Expand All @@ -91,6 +92,22 @@ func (certificate Certificate) Pull(enumerationContext string) (response Respons
return
}

for _, item := range response.Body.PullResponse.PublicKeyCertificateItems {
output := RefinedPublicKeyCertificateResponse{
InstanceID: item.InstanceID,
X509Certificate: item.X509Certificate,
ElementName: item.ElementName,
TrustedRootCertificate: item.TrustedRootCertificate,
Issuer: item.Issuer,
Subject: item.Subject,
ReadOnlyCertificate: item.ReadOnlyCertificate,
}

refinedOutput = append(refinedOutput, output)
}

response.Body.RefinedPullResponse.PublicKeyCertificateItems = refinedOutput

return
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/wsman/amt/publickey/certificate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func TestJson(t *testing.T) {
AddCertificate_OUTPUT: AddCertificate_OUTPUT{},
},
}
expectedResult := "{\"XMLName\":{\"Space\":\"\",\"Local\":\"\"},\"AddTrustedRootCertificate_OUTPUT\":{\"XMLName\":{\"Space\":\"\",\"Local\":\"\"},\"CreatedCertificate\":{\"XMLName\":{\"Space\":\"\",\"Local\":\"\"},\"Address\":\"\",\"ReferenceParameters\":{\"XMLName\":{\"Space\":\"\",\"Local\":\"\"},\"ResourceURI\":\"\",\"SelectorSet\":{\"XMLName\":{\"Space\":\"\",\"Local\":\"\"},\"Selectors\":null}}},\"ReturnValue\":0},\"AddCertificate_OUTPUT\":{\"XMLName\":{\"Space\":\"\",\"Local\":\"\"},\"CreatedCertificate\":{\"XMLName\":{\"Space\":\"\",\"Local\":\"\"},\"Address\":\"\",\"ReferenceParameters\":{\"XMLName\":{\"Space\":\"\",\"Local\":\"\"},\"ResourceURI\":\"\",\"SelectorSet\":{\"XMLName\":{\"Space\":\"\",\"Local\":\"\"},\"Selectors\":null}}},\"ReturnValue\":0},\"AddKey_OUTPUT\":{\"XMLName\":{\"Space\":\"\",\"Local\":\"\"},\"CreatedKey\":{\"XMLName\":{\"Space\":\"\",\"Local\":\"\"},\"Address\":\"\",\"ReferenceParameters\":{\"XMLName\":{\"Space\":\"\",\"Local\":\"\"},\"ResourceURI\":\"\",\"SelectorSet\":{\"XMLName\":{\"Space\":\"\",\"Local\":\"\"},\"Selectors\":null}}},\"ReturnValue\":0},\"GenerateKeyPair_OUTPUT\":{\"XMLName\":{\"Space\":\"\",\"Local\":\"\"},\"KeyPair\":{\"XMLName\":{\"Space\":\"\",\"Local\":\"\"},\"Address\":\"\",\"ReferenceParameters\":{\"XMLName\":{\"Space\":\"\",\"Local\":\"\"},\"ResourceURI\":\"\",\"SelectorSet\":{\"XMLName\":{\"Space\":\"\",\"Local\":\"\"},\"Selectors\":null}}},\"ReturnValue\":0},\"GeneratePKCS10RequestEx_OUTPUT\":{\"XMLName\":{\"Space\":\"\",\"Local\":\"\"},\"SignedCertificateRequest\":\"\",\"ReturnValue\":0},\"KeyManagementGetResponse\":{\"XMLName\":{\"Space\":\"\",\"Local\":\"\"},\"CreationClassName\":\"\",\"ElementName\":\"\",\"EnabledDefault\":0,\"EnabledState\":0,\"Name\":\"\",\"OperationalStatus\":null,\"RequestedState\":0,\"SystemCreationClassName\":\"\",\"SystemName\":\"\"},\"PublicKeyCertificateGetAndPutResponse\":{\"XMLName\":{\"Space\":\"\",\"Local\":\"\"},\"ElementName\":\"\",\"InstanceID\":\"\",\"X509Certificate\":\"\",\"TrustedRootCertificate\":false,\"Issuer\":\"\",\"Subject\":\"\",\"ReadOnlyCertificate\":false},\"EnumerateResponse\":{\"EnumerationContext\":\"\"},\"PullResponse\":{\"XMLName\":{\"Space\":\"\",\"Local\":\"\"},\"KeyManagementItems\":null,\"PublicKeyCertificateItems\":null}}"
expectedResult := "{\"XMLName\":{\"Space\":\"\",\"Local\":\"\"},\"AddTrustedRootCertificate_OUTPUT\":{\"XMLName\":{\"Space\":\"\",\"Local\":\"\"},\"CreatedCertificate\":{\"XMLName\":{\"Space\":\"\",\"Local\":\"\"},\"Address\":\"\",\"ReferenceParameters\":{\"XMLName\":{\"Space\":\"\",\"Local\":\"\"},\"ResourceURI\":\"\",\"SelectorSet\":{\"XMLName\":{\"Space\":\"\",\"Local\":\"\"},\"Selectors\":null}}},\"ReturnValue\":0},\"AddCertificate_OUTPUT\":{\"XMLName\":{\"Space\":\"\",\"Local\":\"\"},\"CreatedCertificate\":{\"XMLName\":{\"Space\":\"\",\"Local\":\"\"},\"Address\":\"\",\"ReferenceParameters\":{\"XMLName\":{\"Space\":\"\",\"Local\":\"\"},\"ResourceURI\":\"\",\"SelectorSet\":{\"XMLName\":{\"Space\":\"\",\"Local\":\"\"},\"Selectors\":null}}},\"ReturnValue\":0},\"AddKey_OUTPUT\":{\"XMLName\":{\"Space\":\"\",\"Local\":\"\"},\"CreatedKey\":{\"XMLName\":{\"Space\":\"\",\"Local\":\"\"},\"Address\":\"\",\"ReferenceParameters\":{\"XMLName\":{\"Space\":\"\",\"Local\":\"\"},\"ResourceURI\":\"\",\"SelectorSet\":{\"XMLName\":{\"Space\":\"\",\"Local\":\"\"},\"Selectors\":null}}},\"ReturnValue\":0},\"GenerateKeyPair_OUTPUT\":{\"XMLName\":{\"Space\":\"\",\"Local\":\"\"},\"KeyPair\":{\"XMLName\":{\"Space\":\"\",\"Local\":\"\"},\"Address\":\"\",\"ReferenceParameters\":{\"XMLName\":{\"Space\":\"\",\"Local\":\"\"},\"ResourceURI\":\"\",\"SelectorSet\":{\"XMLName\":{\"Space\":\"\",\"Local\":\"\"},\"Selectors\":null}}},\"ReturnValue\":0},\"GeneratePKCS10RequestEx_OUTPUT\":{\"XMLName\":{\"Space\":\"\",\"Local\":\"\"},\"SignedCertificateRequest\":\"\",\"ReturnValue\":0},\"KeyManagementGetResponse\":{\"XMLName\":{\"Space\":\"\",\"Local\":\"\"},\"CreationClassName\":\"\",\"ElementName\":\"\",\"EnabledDefault\":0,\"EnabledState\":0,\"Name\":\"\",\"OperationalStatus\":null,\"RequestedState\":0,\"SystemCreationClassName\":\"\",\"SystemName\":\"\"},\"PublicKeyCertificateGetAndPutResponse\":{\"XMLName\":{\"Space\":\"\",\"Local\":\"\"},\"ElementName\":\"\",\"InstanceID\":\"\",\"X509Certificate\":\"\",\"TrustedRootCertificate\":false,\"Issuer\":\"\",\"Subject\":\"\",\"ReadOnlyCertificate\":false},\"EnumerateResponse\":{\"EnumerationContext\":\"\"},\"PullResponse\":{\"XMLName\":{\"Space\":\"\",\"Local\":\"\"},\"KeyManagementItems\":null,\"PublicKeyCertificateItems\":null},\"RefinedPullResponse\":{}}"
result := response.JSON()
assert.Equal(t, expectedResult, result)
}
Expand All @@ -37,7 +37,7 @@ func TestYaml(t *testing.T) {
AddCertificate_OUTPUT: AddCertificate_OUTPUT{},
},
}
expectedResult := "xmlname:\n space: \"\"\n local: \"\"\naddtrustedrootcertificate_output:\n xmlname:\n space: \"\"\n local: \"\"\n createdcertificate:\n xmlname:\n space: \"\"\n local: \"\"\n address: \"\"\n referenceparameters:\n xmlname:\n space: \"\"\n local: \"\"\n resourceuri: \"\"\n selectorset:\n xmlname:\n space: \"\"\n local: \"\"\n selectors: []\n returnvalue: 0\naddcertificate_output:\n xmlname:\n space: \"\"\n local: \"\"\n createdcertificate:\n xmlname:\n space: \"\"\n local: \"\"\n address: \"\"\n referenceparameters:\n xmlname:\n space: \"\"\n local: \"\"\n resourceuri: \"\"\n selectorset:\n xmlname:\n space: \"\"\n local: \"\"\n selectors: []\n returnvalue: 0\naddkey_output:\n xmlname:\n space: \"\"\n local: \"\"\n createdkey:\n xmlname:\n space: \"\"\n local: \"\"\n address: \"\"\n referenceparameters:\n xmlname:\n space: \"\"\n local: \"\"\n resourceuri: \"\"\n selectorset:\n xmlname:\n space: \"\"\n local: \"\"\n selectors: []\n returnvalue: 0\ngeneratekeypair_output:\n xmlname:\n space: \"\"\n local: \"\"\n keypair:\n xmlname:\n space: \"\"\n local: \"\"\n address: \"\"\n referenceparameters:\n xmlname:\n space: \"\"\n local: \"\"\n resourceuri: \"\"\n selectorset:\n xmlname:\n space: \"\"\n local: \"\"\n selectors: []\n returnvalue: 0\ngeneratepkcs10requestex_output:\n xmlname:\n space: \"\"\n local: \"\"\n signedcertificaterequest: \"\"\n returnvalue: 0\nkeymanagementgetresponse:\n xmlname:\n space: \"\"\n local: \"\"\n creationclassname: \"\"\n elementname: \"\"\n enableddefault: 0\n enabledstate: 0\n name: \"\"\n operationalstatus: []\n requestedstate: 0\n systemcreationclassname: \"\"\n systemname: \"\"\npublickeycertificategetandputresponse:\n xmlname:\n space: \"\"\n local: \"\"\n elementname: \"\"\n instanceid: \"\"\n x509certificate: \"\"\n trustedrootcertificate: false\n issuer: \"\"\n subject: \"\"\n readonlycertificate: false\nenumerateresponse:\n enumerationcontext: \"\"\npullresponse:\n xmlname:\n space: \"\"\n local: \"\"\n keymanagementitems: []\n publickeycertificateitems: []\n"
expectedResult := "xmlname:\n space: \"\"\n local: \"\"\naddtrustedrootcertificate_output:\n xmlname:\n space: \"\"\n local: \"\"\n createdcertificate:\n xmlname:\n space: \"\"\n local: \"\"\n address: \"\"\n referenceparameters:\n xmlname:\n space: \"\"\n local: \"\"\n resourceuri: \"\"\n selectorset:\n xmlname:\n space: \"\"\n local: \"\"\n selectors: []\n returnvalue: 0\naddcertificate_output:\n xmlname:\n space: \"\"\n local: \"\"\n createdcertificate:\n xmlname:\n space: \"\"\n local: \"\"\n address: \"\"\n referenceparameters:\n xmlname:\n space: \"\"\n local: \"\"\n resourceuri: \"\"\n selectorset:\n xmlname:\n space: \"\"\n local: \"\"\n selectors: []\n returnvalue: 0\naddkey_output:\n xmlname:\n space: \"\"\n local: \"\"\n createdkey:\n xmlname:\n space: \"\"\n local: \"\"\n address: \"\"\n referenceparameters:\n xmlname:\n space: \"\"\n local: \"\"\n resourceuri: \"\"\n selectorset:\n xmlname:\n space: \"\"\n local: \"\"\n selectors: []\n returnvalue: 0\ngeneratekeypair_output:\n xmlname:\n space: \"\"\n local: \"\"\n keypair:\n xmlname:\n space: \"\"\n local: \"\"\n address: \"\"\n referenceparameters:\n xmlname:\n space: \"\"\n local: \"\"\n resourceuri: \"\"\n selectorset:\n xmlname:\n space: \"\"\n local: \"\"\n selectors: []\n returnvalue: 0\ngeneratepkcs10requestex_output:\n xmlname:\n space: \"\"\n local: \"\"\n signedcertificaterequest: \"\"\n returnvalue: 0\nkeymanagementgetresponse:\n xmlname:\n space: \"\"\n local: \"\"\n creationclassname: \"\"\n elementname: \"\"\n enableddefault: 0\n enabledstate: 0\n name: \"\"\n operationalstatus: []\n requestedstate: 0\n systemcreationclassname: \"\"\n systemname: \"\"\npublickeycertificategetandputresponse:\n xmlname:\n space: \"\"\n local: \"\"\n elementname: \"\"\n instanceid: \"\"\n x509certificate: \"\"\n trustedrootcertificate: false\n issuer: \"\"\n subject: \"\"\n readonlycertificate: false\nenumerateresponse:\n enumerationcontext: \"\"\npullresponse:\n xmlname:\n space: \"\"\n local: \"\"\n keymanagementitems: []\n publickeycertificateitems: []\nrefinedpullresponse:\n keymanagementitems: []\n publickeycertificateitems: []\n"
result := response.YAML()
assert.Equal(t, expectedResult, result)
}
Expand Down
26 changes: 26 additions & 0 deletions pkg/wsman/amt/publickey/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ type (
PublicKeyCertificateGetAndPutResponse PublicKeyCertificateResponse `xml:"AMT_PublicKeyCertificate,omitempty"`
EnumerateResponse common.EnumerateResponse
PullResponse PullResponse
RefinedPullResponse RefinedPullResponse
}
RefinedPullResponse struct {
KeyManagementItems []RefinedKeyManagementResponse `json:"KeyManagementItems,omitempty"`
PublicKeyCertificateItems []RefinedPublicKeyCertificateResponse `json:"PublicKeyCertificateItems,omitempty"`
}
PullResponse struct {
XMLName xml.Name `xml:"PullResponse,omitempty"`
Expand All @@ -59,6 +64,17 @@ type (
SystemCreationClassName string `xml:"SystemCreationClassName,omitempty"` // The CreationClassName of the scoping System.
SystemName string `xml:"SystemName,omitempty"` // The Name of the scoping System.
}
RefinedKeyManagementResponse struct {
CreationClassName string
ElementName string
EnabledDefault EnabledDefault
EnabledState EnabledState
Name string
RequestedState RequestedState
SystemCreationClassName string
SystemName string
}

PublicKeyCertificateResponse struct {
XMLName xml.Name `xml:"AMT_PublicKeyCertificate,omitempty"`
ElementName string `xml:"ElementName,omitempty"` // A user-friendly name for the object . . .
Expand All @@ -69,6 +85,16 @@ type (
Subject string `xml:"Subject,omitempty"` // The Subject field of this certificate.
ReadOnlyCertificate bool `xml:"ReadOnlyCertificate"` // Indicates whether the certificate is an Intel AMT self-signed certificate. If True, the certificate cannot be deleted.
}

RefinedPublicKeyCertificateResponse struct {
ElementName string `xml:"ElementName,omitempty"` // A user-friendly name for the object . . .
InstanceID string `xml:"InstanceID,omitempty"` // Within the scope of the instantiating Namespace, InstanceID opaquely and uniquely identifies an instance of this class.
X509Certificate string `xml:"X509Certificate,omitempty"` // uint8[4100] // The X.509 Certificate blob.
TrustedRootCertificate bool `xml:"TrustedRootCertficate"` // For root certificate [that were added by AMT_PublicKeyManagementService.AddTrustedRootCertificate()]this property will be true. FYI Certificate is spelled wrong comimg from AMT.
Issuer string `xml:"Issuer,omitempty"` // The Issuer field of this certificate.
Subject string `xml:"Subject,omitempty"` // The Subject field of this certificate.
ReadOnlyCertificate bool `xml:"ReadOnlyCertificate"` // Indicates whether the certificate is an Intel AMT self-signed certificate. If True, the certificate cannot be deleted.
}
AddTrustedRootCertificate_OUTPUT struct {
XMLName xml.Name `xml:"AddTrustedRootCertificate_OUTPUT"`
CreatedCertificate CreatedCertificateResponse `xml:"CreatedCertificate,omitempty"`
Expand Down
12 changes: 12 additions & 0 deletions pkg/wsman/amt/publicprivate/keypair.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ func (keyPair KeyPair) Enumerate() (response Response, err error) {

// Pull returns the instances of this class. An enumeration context provided by the Enumerate call is used as input.
func (keyPair KeyPair) Pull(enumerationContext string) (response Response, err error) {
var refinedOutput []RefinedPublicPrivateKeyPair
response = Response{
Message: &client.Message{
XMLInput: keyPair.base.Pull(enumerationContext),
Expand All @@ -85,6 +86,17 @@ func (keyPair KeyPair) Pull(enumerationContext string) (response Response, err e
if err != nil {
return
}
for _, item := range response.Body.PullResponse.PublicPrivateKeyPairItems {

Check failure on line 89 in pkg/wsman/amt/publicprivate/keypair.go

View workflow job for this annotation

GitHub Actions / runner / golangci-lint

[golangci] reported by reviewdog 🐶 only one cuddle assignment allowed before range statement (wsl) Raw Output: pkg/wsman/amt/publicprivate/keypair.go:89:2: only one cuddle assignment allowed before range statement (wsl) for _, item := range response.Body.PullResponse.PublicPrivateKeyPairItems { ^
output := RefinedPublicPrivateKeyPair{
InstanceID: item.InstanceID,
ElementName: item.ElementName,
DERKey: item.DERKey,
}

refinedOutput = append(refinedOutput, output)
}

response.Body.RefinedPullResponse.PublicPrivateKeyPairItems = refinedOutput

return
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/wsman/amt/publicprivate/keypair_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestJson(t *testing.T) {
GetResponse: PublicPrivateKeyPair{},
},
}
expectedResult := "{\"XMLName\":{\"Space\":\"\",\"Local\":\"\"},\"GetResponse\":{\"XMLName\":{\"Space\":\"\",\"Local\":\"\"},\"ElementName\":\"\",\"InstanceID\":\"\",\"DERKey\":\"\"},\"EnumerateResponse\":{\"EnumerationContext\":\"\"},\"PullResponse\":{\"XMLName\":{\"Space\":\"\",\"Local\":\"\"},\"PublicPrivateKeyPairItems\":null}}"
expectedResult := "{\"XMLName\":{\"Space\":\"\",\"Local\":\"\"},\"GetResponse\":{\"XMLName\":{\"Space\":\"\",\"Local\":\"\"},\"ElementName\":\"\",\"InstanceID\":\"\",\"DERKey\":\"\"},\"EnumerateResponse\":{\"EnumerationContext\":\"\"},\"PullResponse\":{\"XMLName\":{\"Space\":\"\",\"Local\":\"\"},\"PublicPrivateKeyPairItems\":null},\"RefinedPullResponse\":{\"PublicPrivateKeyPairItems\":null}}"
result := response.JSON()
assert.Equal(t, expectedResult, result)
}
Expand All @@ -34,7 +34,7 @@ func TestYaml(t *testing.T) {
GetResponse: PublicPrivateKeyPair{},
},
}
expectedResult := "xmlname:\n space: \"\"\n local: \"\"\ngetresponse:\n xmlname:\n space: \"\"\n local: \"\"\n elementname: \"\"\n instanceid: \"\"\n derkey: \"\"\nenumerateresponse:\n enumerationcontext: \"\"\npullresponse:\n xmlname:\n space: \"\"\n local: \"\"\n publicprivatekeypairitems: []\n"
expectedResult := "xmlname:\n space: \"\"\n local: \"\"\ngetresponse:\n xmlname:\n space: \"\"\n local: \"\"\n elementname: \"\"\n instanceid: \"\"\n derkey: \"\"\nenumerateresponse:\n enumerationcontext: \"\"\npullresponse:\n xmlname:\n space: \"\"\n local: \"\"\n publicprivatekeypairitems: []\nrefinedpullresponse:\n publicprivatekeypairitems: []\n"
result := response.YAML()
assert.Equal(t, expectedResult, result)
}
Expand Down
Loading

0 comments on commit b962771

Please sign in to comment.