-
Notifications
You must be signed in to change notification settings - Fork 14
/
option.go
151 lines (131 loc) · 4.54 KB
/
option.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
// Copyright 2021 Converter Systems LLC. All rights reserved.
package client
import (
"crypto/rsa"
"crypto/tls"
"github.com/awcullen/opcua/ua"
)
// Option is a functional option to be applied to a client during initialization.
type Option func(*Client) error
// WithSecurityPolicyURI selects endpoint with given security policy URI. (default: "" selects most secure endpoint)
func WithSecurityPolicyURI(uri string) Option {
return func(c *Client) error {
c.securityPolicyURI = uri
return nil
}
}
// WithUserNameIdentity sets the user identity to a UserNameIdentity created from a username and password. (default: AnonymousIdentity)
func WithUserNameIdentity(userName, password string) Option {
return func(c *Client) error {
c.userIdentity = ua.UserNameIdentity{UserName: userName, Password: password}
return nil
}
}
// WithX509Identity sets the user identity to an X509Identity created from a certificate and private key. (default: AnonymousIdentity)
func WithX509Identity(certificate ua.ByteString, privateKey *rsa.PrivateKey) Option {
return func(c *Client) error {
c.userIdentity = ua.X509Identity{Certificate: certificate, Key: privateKey}
return nil
}
}
// WithIssuedIdentity sets the user identity to an IssuedIdentity created from a token. (default: AnonymousIdentity)
func WithIssuedIdentity(tokenData ua.ByteString) Option {
return func(c *Client) error {
c.userIdentity = ua.IssuedIdentity{TokenData: tokenData}
return nil
}
}
// WithApplicationName sets the name of the client application. (default: package name)
func WithApplicationName(value string) Option {
return func(c *Client) error {
c.applicationName = value
return nil
}
}
// WithSessionName sets the name of the session. (default: server assigned)
func WithSessionName(value string) Option {
return func(c *Client) error {
c.sessionName = value
return nil
}
}
// WithSessionTimeout sets the number of milliseconds that a session may be unused before being closed by the server. (default: 2 min)
func WithSessionTimeout(value float64) Option {
return func(c *Client) error {
c.sessionTimeout = value
return nil
}
}
// WithClientCertificate sets the client certificate and private key.
func WithClientCertificate(cert []byte, privateKey *rsa.PrivateKey) Option {
return func(c *Client) error {
var err error
c.localCertificate, c.localPrivateKey = cert, privateKey
return err
}
}
// WithClientCertificateFile sets the file paths of the client certificate and private key.
// Reads and parses a public/private key pair from a pair of files. The files must contain PEM encoded data.
func WithClientCertificateFile(certPath, keyPath string) Option {
return func(c *Client) error {
cert, err := tls.LoadX509KeyPair(certPath, keyPath)
if err != nil {
return err
}
c.localCertificate = cert.Certificate[0]
c.localPrivateKey, _ = cert.PrivateKey.(*rsa.PrivateKey)
return nil
}
}
// WithTrustedCertificatesFile sets the file path of the trusted server certificates or certificate authorities.
// The files must contain PEM encoded data.
func WithTrustedCertificatesFile(path string) Option {
return func(c *Client) error {
c.trustedCertsFile = path
return nil
}
}
// WithInsecureSkipVerify skips verification of server certificate. Skips checking HostName, Expiration, and Authority.
func WithInsecureSkipVerify() Option {
return func(c *Client) error {
c.suppressHostNameInvalid = true
c.suppressCertificateExpired = true
c.suppressCertificateChainIncomplete = true
return nil
}
}
// WithTimeoutHint sets the default number of milliseconds to wait before the ServiceRequest is cancelled. (default: 1500)
func WithTimeoutHint(value uint32) Option {
return func(c *Client) error {
c.timeoutHint = value
return nil
}
}
// WithDiagnosticsHint sets the default diagnostic hint that is sent in a request. (default: None)
func WithDiagnosticsHint(value uint32) Option {
return func(c *Client) error {
c.diagnosticsHint = value
return nil
}
}
// WithTokenLifetime sets the requested number of milliseconds before a security token is renewed. (default: 60 min)
func WithTokenLifetime(value uint32) Option {
return func(c *Client) error {
c.tokenLifetime = value
return nil
}
}
// WithConnectTimeout sets the number of milliseconds to wait for a connection response. (default:5000)
func WithConnectTimeout(value int64) Option {
return func(c *Client) error {
c.connectTimeout = value
return nil
}
}
// WithTrace logs all ServiceRequests and ServiceResponses to StdOut.
func WithTrace() Option {
return func(c *Client) error {
c.trace = true
return nil
}
}