forked from stmcginnis/gofish
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoutboundconnection.go
221 lines (190 loc) · 10.1 KB
/
outboundconnection.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
//
// SPDX-License-Identifier: BSD-3-Clause
//
package redfish
import (
"encoding/json"
"reflect"
"github.com/stmcginnis/gofish/common"
)
type AuthenticationType string
const (
// MTLSAuthenticationType shall indicate the service will exchange and verify certificates during TLS handshaking
// when establishing the outbound connecting.
MTLSAuthenticationType AuthenticationType = "MTLS"
// JWTAuthenticationType shall indicate an RFC7519-defined JSON Web Token (JWT) is specified in one of the HTTP
// headers in the PreUpgradeHTTPHeaders property. This is typically encoded in the 'Authorization' header with the
// scheme 'Bearer'.
JWTAuthenticationType AuthenticationType = "JWT"
// NoneAuthenticationType shall indicate the service does not provide any authentication information to the remote
// client.
NoneAuthenticationType AuthenticationType = "None"
// OEMAuthenticationType shall indicate an OEM-specific authentication mechanism.
OEMAuthenticationType AuthenticationType = "OEM"
)
type OutboundConnectionRetryPolicyType string
const (
// NoneOutboundConnectionRetryPolicyType shall indicate the service will not attempt to re-establish the outbound
// connection if the connection is dropped or not established. If the connection is dropped or not established, the
// service shall set the ConnectionEnabled property to 'false'.
NoneOutboundConnectionRetryPolicyType OutboundConnectionRetryPolicyType = "None"
// RetryForeverOutboundConnectionRetryPolicyType shall indicate the service will attempt to re-establish the
// outbound connection at the interval specified by the RetryIntervalMinutes property regardless of the number of
// retries.
RetryForeverOutboundConnectionRetryPolicyType OutboundConnectionRetryPolicyType = "RetryForever"
// RetryCountOutboundConnectionRetryPolicyType shall indicate the service will attempt to re-establish the outbound
// connection at the interval specified by the RetryIntervalMinutes property until the number of retries reaches
// the count specified by the RetryCount property. If the limit is reached, the service shall set the
// ConnectionEnabled property to 'false'. If a connection is established, the service shall reset the count.
RetryCountOutboundConnectionRetryPolicyType OutboundConnectionRetryPolicyType = "RetryCount"
)
// OutboundConnection shall represent the connection configuration necessary to connect to a remote client.
// Services shall initiate the outbound connection over a WebSocket defined in the 'Outbound connections' clause of
// the Redfish Specification.
type OutboundConnection struct {
common.Entity
// ODataContext is the odata context.
ODataContext string `json:"@odata.context"`
// ODataEtag is the odata etag.
ODataEtag string `json:"@odata.etag"`
// ODataType is the odata type.
ODataType string `json:"@odata.type"`
// Authentication shall contain the authentication mechanism for the WebSocket connection.
Authentication AuthenticationType
// Certificates shall contain a link to a resource collection of type CertificateCollection that represents the
// server certificates for the remote client referenced by the EndpointURI property. If the Authentication property
// contains 'MTLS', the service shall compare the certificates in this collection with the certificate obtained
// during handshaking with the WebSocket service to verify the identity of the remote client prior to completing
// the connection. If the remote client cannot be verified, the service shall not complete the connection.
// Regardless of the contents of this collection, services may perform additional verification based on other
// factors, such as the configuration of the SecurityPolicy resource.
certificates string
// ClientCertificates shall contain a link to a resource collection of type CertificateCollection that represents
// the client identity certificates for the service. If the Authentication property contains 'MTLS', these
// certificates are provided to the remote client referenced by the EndpointURI property as part of TLS
// handshaking.
clientCertificates string
// ConnectionEnabled shall indicate if the outbound connection is enabled. If 'true', the service shall attempt to
// establish an outbound connection to the remote client specified by the EndpointURI property. If 'false', the
// service shall not attempt to establish a connection to the remote client and shall close the connection if one
// is already established. When a connection is established, the service shall create a Session resource to
// represent the active connection. When a connection is closed, the service shall delete the connection's
// respective Session resource. If the client does not provide this property, the service shall default this value
// to 'true'.
ConnectionEnabled bool
// Description provides a description of this resource.
Description string
// EndpointURI shall contain the WebSocket URI to the external web service of the remote client. The value shall
// follow the URI format defined in RFC6455. Services shall reject URIs that do not contain the scheme 'wss'.
EndpointURI string
// Oem shall contain the OEM extensions. All values for properties that this object contains shall conform to the
// Redfish Specification-described requirements.
OEM json.RawMessage `json:"Oem"`
// PreUpgradeHTTPHeaders shall contain an object consisting of the names and values of HTTP headers to send to the
// remote client during the initial connection prior to the WebSocket upgrade. This property shall be an empty
// object in responses.
PreUpgradeHTTPHeaders map[string]string
// RetryPolicy shall contain the retry policy for this outbound connection. If not specified by the client in the
// create request, the service shall assume ConnectionRetryPolicy contains 'None'.
RetryPolicy RetryPolicyType
// Roles shall contain the Redfish roles that contain the privileges of the remote client for the outbound
// connection.
Roles []string
// Status shall contain any status or health properties of the resource.
Status common.Status
// WebSocketPingIntervalMinutes shall contain the interval for the service to send the WebSocket ping opcode to the
// remote client in minutes. If '0', the service shall not send the WebSocket ping opcode to the remote client.
WebSocketPingIntervalMinutes int
// rawData holds the original serialized JSON so we can compare updates.
rawData []byte
session string
}
// UnmarshalJSON unmarshals a OutboundConnection object from the raw JSON.
func (outboundconnection *OutboundConnection) UnmarshalJSON(b []byte) error {
type temp OutboundConnection
type Links struct {
// Session shall contain the link to a resource of type Session that represents the active connection for this
// outbound connection.
Session common.Link
}
var t struct {
temp
Links Links
Certificates common.Link
ClientCertificates common.Link
}
err := json.Unmarshal(b, &t)
if err != nil {
return err
}
*outboundconnection = OutboundConnection(t.temp)
// Extract the links to other entities for later
outboundconnection.session = t.Links.Session.String()
outboundconnection.certificates = t.Certificates.String()
outboundconnection.clientCertificates = t.ClientCertificates.String()
// This is a read/write object, so we need to save the raw object data for later
outboundconnection.rawData = b
return nil
}
// Session gets the the active connection for this outbound connection.
func (outboundconnection *OutboundConnection) Session() (*Session, error) {
if outboundconnection.session == "" {
return nil, nil
}
return GetSession(outboundconnection.GetClient(), outboundconnection.session)
}
// Certificates gets the server certificates for the remote client referenced by the EndpointURI property.
func (outboundconnection *OutboundConnection) Certificates() ([]*Certificate, error) {
return ListReferencedCertificates(outboundconnection.GetClient(), outboundconnection.certificates)
}
// ClientCertificates gets the client identity certificates for the service.
func (outboundconnection *OutboundConnection) ClientCertificates() ([]*Certificate, error) {
return ListReferencedCertificates(outboundconnection.GetClient(), outboundconnection.clientCertificates)
}
// Update commits updates to this object's properties to the running system.
func (outboundconnection *OutboundConnection) Update() error {
// Get a representation of the object's original state so we can find what
// to update.
original := new(OutboundConnection)
original.UnmarshalJSON(outboundconnection.rawData)
readWriteFields := []string{
"ConnectionEnabled",
"WebSocketPingIntervalMinutes",
}
originalElement := reflect.ValueOf(original).Elem()
currentElement := reflect.ValueOf(outboundconnection).Elem()
return outboundconnection.Entity.Update(originalElement, currentElement, readWriteFields)
}
// GetOutboundConnection will get a OutboundConnection instance from the service.
func GetOutboundConnection(c common.Client, uri string) (*OutboundConnection, error) {
return common.GetObject[OutboundConnection](c, uri)
}
// ListReferencedOutboundConnections gets the collection of OutboundConnection from
// a provided reference.
func ListReferencedOutboundConnections(c common.Client, link string) ([]*OutboundConnection, error) {
return common.GetCollectionObjects[OutboundConnection](c, link)
}
// RetryPolicyType shall contain the retry policy for an outbound connection.
type RetryPolicyType struct {
// ConnectionRetryPolicy shall contain the type of retry policy for this outbound connection.
ConnectionRetryPolicy OutboundConnectionRetryPolicyType
// RetryCount shall contain the number of retries to attempt if the retry policy specifies a maximum number of
// retries.
RetryCount int
// RetryIntervalMinutes shall contain the interval for the service to retry connecting to remote client in minutes.
RetryIntervalMinutes int
}
// UnmarshalJSON unmarshals a RetryPolicyType object from the raw JSON.
func (retrypolicytype *RetryPolicyType) UnmarshalJSON(b []byte) error {
type temp RetryPolicyType
var t struct {
temp
}
err := json.Unmarshal(b, &t)
if err != nil {
return err
}
*retrypolicytype = RetryPolicyType(t.temp)
// Extract the links to other entities for later
return nil
}