async-opcua 0.18.0

OPC UA client and server API
Documentation
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
use std::{
    sync::{atomic::Ordering, Arc},
    time::Duration,
};

use super::utils::hostname;
use async_trait::async_trait;
use bytes::BytesMut;
use log::debug;
use opcua::{
    client::IdentityToken,
    core::comms::tcp_codec::{Message, TcpCodec},
    core::config::Config,
    crypto::SecurityPolicy,
    types::{
        ApplicationType, DecodingOptions, MessageSecurityMode, NodeId, ReadValueId, StatusCode,
        TimestampsToReturn, VariableId, Variant,
    },
};
use opcua_client::IssuedTokenWrapper;
use opcua_server::{
    authenticator::{issued_token_security_policy, AuthManager, UserToken},
    ServerEndpoint,
};
use opcua_types::{ByteString, Error, UAString, UserTokenPolicy, UserTokenType};
use tokio::{
    io::AsyncReadExt,
    net::{TcpListener, TcpStream},
};
use tokio_util::codec::Decoder;

use crate::utils::{
    client_user_token, client_x509_token, copy_shared_certs, default_server, test_server, Tester,
    CLIENT_USERPASS_ID, TEST_COUNTER,
};

#[tokio::test]
async fn hello_timeout() {
    let _ = env_logger::try_init();

    let test_id = TEST_COUNTER.fetch_add(1, Ordering::Relaxed);
    let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
    let port = listener.local_addr().unwrap().port();

    let server = default_server()
        .discovery_urls(vec![format!("opc.tcp://{}:{}", hostname(), port)])
        .pki_dir(format!("./pki-server/{test_id}"))
        .hello_timeout(1);
    copy_shared_certs(test_id, &server.config().application_description());

    let (server, handle) = server.build().unwrap();
    let addr = listener.local_addr().unwrap();

    tokio::task::spawn(server.run_with(listener));

    let _guard = handle.token().clone().drop_guard();

    let mut stream = TcpStream::connect(addr).await.unwrap();
    debug!("Connected to {addr}");

    // Wait a bit more than the hello timeout (1 second)
    tokio::time::sleep(Duration::from_millis(1200)).await;

    let mut bytes = BytesMut::with_capacity(1024);
    let result = stream.read_buf(&mut bytes).await;
    // Should first read the error message from the server.
    let read = result.unwrap();
    assert!(read > 0);
    let mut codec = TcpCodec::new(DecodingOptions::default());
    let msg = codec.decode(&mut bytes).unwrap();
    let Some(Message::Error(msg)) = msg else {
        panic!("Expected error got {msg:?}");
    };
    assert_eq!(msg.error, StatusCode::BadTimeout);

    let result = stream.read_buf(&mut bytes).await;

    match result {
        Ok(v) => {
            if v > 0 {
                panic!("Hello timeout exceeded and socket is still open, result = {v}")
            } else {
                // From
                debug!("Client got a read of 0 bytes on the socket, so treating by terminating with success");
            }
        }
        Err(err) => {
            debug!("Client got an error {err:?} on the socket terminating successfully");
        }
    }
    debug!("Test passed, closing server");
}

#[tokio::test]
async fn get_endpoints() {
    let tester = Tester::new_default_server(false).await;
    let endpoints = tester
        .client
        .get_server_endpoints_from_url(tester.endpoint())
        .await
        .unwrap();
    assert_eq!(endpoints.len(), tester.handle.info().config.endpoints.len());
}

async fn conn_test(policy: SecurityPolicy, mode: MessageSecurityMode, token: IdentityToken) {
    let mut tester = Tester::new_default_server(false).await;
    let (session, handle) = tester.connect(policy, mode, token).await.unwrap();
    let _h = handle.spawn();

    tokio::time::timeout(Duration::from_secs(20), session.wait_for_connection())
        .await
        .unwrap();

    session
        .read(
            &[ReadValueId::from(<VariableId as Into<NodeId>>::into(
                VariableId::Server_ServiceLevel,
            ))],
            TimestampsToReturn::Both,
            0.0,
        )
        .await
        .unwrap();
}

#[tokio::test]
async fn connect_none() {
    conn_test(
        SecurityPolicy::None,
        MessageSecurityMode::None,
        IdentityToken::Anonymous,
    )
    .await;
}

#[tokio::test]
async fn connect_basic128rsa15_sign() {
    conn_test(
        SecurityPolicy::Basic128Rsa15,
        MessageSecurityMode::Sign,
        IdentityToken::Anonymous,
    )
    .await;
}

#[tokio::test]
async fn connect_basic128rsa15_sign_and_encrypt() {
    conn_test(
        SecurityPolicy::Basic128Rsa15,
        MessageSecurityMode::SignAndEncrypt,
        IdentityToken::Anonymous,
    )
    .await;
}

#[tokio::test]
async fn connect_basic256_sign() {
    conn_test(
        SecurityPolicy::Basic256,
        MessageSecurityMode::Sign,
        IdentityToken::Anonymous,
    )
    .await;
}

#[tokio::test]
async fn connect_basic256_sign_and_encrypt() {
    conn_test(
        SecurityPolicy::Basic256,
        MessageSecurityMode::SignAndEncrypt,
        IdentityToken::Anonymous,
    )
    .await;
}

#[tokio::test]
async fn connect_aes256sha256rsaoaep_sign() {
    conn_test(
        SecurityPolicy::Aes128Sha256RsaOaep,
        MessageSecurityMode::Sign,
        IdentityToken::Anonymous,
    )
    .await;
}

#[tokio::test]
async fn connect_aes256sha256rsaoaep_sign_and_encrypt() {
    conn_test(
        SecurityPolicy::Aes128Sha256RsaOaep,
        MessageSecurityMode::SignAndEncrypt,
        IdentityToken::Anonymous,
    )
    .await;
}

#[tokio::test]
async fn connect_aes256sha256rsapss_sign() {
    conn_test(
        SecurityPolicy::Aes256Sha256RsaPss,
        MessageSecurityMode::Sign,
        IdentityToken::Anonymous,
    )
    .await;
}

#[tokio::test]
async fn connect_aes256sha256rsapss_sign_and_encrypt() {
    conn_test(
        SecurityPolicy::Aes256Sha256RsaPss,
        MessageSecurityMode::SignAndEncrypt,
        IdentityToken::Anonymous,
    )
    .await;
}

#[tokio::test]
async fn connect_basic128rsa15_with_username_password() {
    conn_test(
        SecurityPolicy::Basic128Rsa15,
        MessageSecurityMode::SignAndEncrypt,
        client_user_token(),
    )
    .await;
}

#[tokio::test]
async fn connect_basic128rsa15_with_x509_token() {
    conn_test(
        SecurityPolicy::Basic128Rsa15,
        MessageSecurityMode::SignAndEncrypt,
        client_x509_token().unwrap(),
    )
    .await;
}

#[tokio::test]
async fn connect_basic128rsa_15_with_invalid_token() {
    let mut tester = Tester::new_default_server(true).await;
    let (_, handle) = tester
        .connect(
            SecurityPolicy::Basic128Rsa15,
            MessageSecurityMode::SignAndEncrypt,
            IdentityToken::UserName(CLIENT_USERPASS_ID.to_owned(), "invalid".into()),
        )
        .await
        .unwrap();
    let res = handle.spawn().await.unwrap();
    assert_eq!(res, StatusCode::BadIdentityTokenRejected);
}

#[tokio::test]
async fn find_servers() {
    let tester = Tester::new_default_server(true).await;
    let servers = tester
        .client
        .find_servers(tester.endpoint(), None, None)
        .await
        .unwrap();
    assert_eq!(servers.len(), 1);

    let s = &servers[0];
    let discovery_urls = s.discovery_urls.as_ref().unwrap();
    assert!(!discovery_urls.is_empty());
    assert_eq!(s.application_type, ApplicationType::Server);
    assert_eq!(s.application_name.text.as_ref(), "integration_server");
    assert_eq!(s.application_uri.as_ref(), "urn:integration_server");
    assert_eq!(s.product_uri.as_ref(), "urn:integration_server Testkit");
}

#[tokio::test]
async fn discovery_test() {
    let tester = Tester::new_default_server(true).await;
    // Get all
    let endpoints = tester
        .client
        .get_endpoints(tester.endpoint(), &[], &[])
        .await
        .unwrap();
    assert_eq!(endpoints.len(), 11);

    // Get with wrong profile URIs
    let endpoints = tester
        .client
        .get_endpoints(tester.endpoint(), &[], &["wrongwrong"])
        .await
        .unwrap();
    assert!(endpoints.is_empty());

    // Get all binary endpoints (all of them)
    let endpoints = tester
        .client
        .get_endpoints(
            tester.endpoint(),
            &[],
            &["http://opcfoundation.org/UA-Profile/Transport/uatcp-uasc-uabinary"],
        )
        .await
        .unwrap();
    assert_eq!(endpoints.len(), 11);
}

#[tokio::test]
async fn multi_client_test() {
    // Simple multi-client test, checking that we can send and receive requests with multiple clients
    // to the same server, and also that the client SDK can handle multiple sessions in the same client.
    let mut tester = Tester::new_default_server(true).await;

    let c1 = tester
        .connect_and_wait(
            SecurityPolicy::Basic128Rsa15,
            MessageSecurityMode::SignAndEncrypt,
            IdentityToken::UserName(
                CLIENT_USERPASS_ID.to_owned(),
                format!("{CLIENT_USERPASS_ID}_password").into(),
            ),
        )
        .await
        .unwrap();
    // Same user token, should still be fine
    let c2 = tester
        .connect_and_wait(
            SecurityPolicy::Basic256,
            MessageSecurityMode::SignAndEncrypt,
            IdentityToken::UserName(
                CLIENT_USERPASS_ID.to_owned(),
                format!("{CLIENT_USERPASS_ID}_password").into(),
            ),
        )
        .await
        .unwrap();

    // Different user, anonymous
    let c3 = tester
        .connect_and_wait(
            SecurityPolicy::None,
            MessageSecurityMode::None,
            IdentityToken::Anonymous,
        )
        .await
        .unwrap();

    // Read the service level a few times
    let mut val = 100;
    for _ in 0..5 {
        val += 10;
        tester.handle.set_service_level(val);
        for session in &[c1.clone(), c2.clone(), c3.clone()] {
            let value = session
                .read(
                    &[ReadValueId::from(<VariableId as Into<NodeId>>::into(
                        VariableId::Server_ServiceLevel,
                    ))],
                    TimestampsToReturn::Both,
                    0.0,
                )
                .await
                .unwrap();
            let Some(Variant::Byte(v)) = value[0].value else {
                panic!("Wrong result type");
            };
            assert_eq!(val, v);
        }
    }
}

#[tokio::test]
async fn recoverable_error_test_server() {
    // Test that if we send a too large message to the server, we don't lose the connection
    // entirely.
    let mut server = test_server();
    server = server.max_array_length(50);
    let mut tester = Tester::new(server, false).await;
    let (session, lp) = tester.connect_default().await.unwrap();
    lp.spawn();
    tokio::time::timeout(Duration::from_secs(2), session.wait_for_connection())
        .await
        .unwrap();

    let ids = (0..100)
        .map(|_| {
            ReadValueId::from(<VariableId as Into<NodeId>>::into(
                VariableId::Server_ServiceLevel,
            ))
        })
        .collect::<Vec<_>>();

    let res = session
        .read(&ids, TimestampsToReturn::Both, 0.0)
        .await
        .unwrap_err();
    assert_eq!(res, StatusCode::BadDecodingError);

    session
        .read(
            &[ReadValueId::from(<VariableId as Into<NodeId>>::into(
                VariableId::Server_ServiceLevel,
            ))],
            TimestampsToReturn::Both,
            0.0,
        )
        .await
        .unwrap();
}

struct IssuedTokenAuthenticator;

#[async_trait]
impl AuthManager for IssuedTokenAuthenticator {
    fn user_token_policies(&self, endpoint: &ServerEndpoint) -> Vec<UserTokenPolicy> {
        if endpoint.path == "/issued_token" {
            vec![UserTokenPolicy {
                policy_id: issued_token_security_policy(endpoint),
                token_type: UserTokenType::IssuedToken,
                issued_token_type: opcua::types::issued_token_types::JSON_WEB_TOKEN.into(),
                // Yes this is JSON in a string. The real thing would have a lot more fields.
                issuer_endpoint_url: "{\"ua:tokenEndpoint\": \"example.com/token\"}".into(),
                security_policy_uri: UAString::null(),
            }]
        } else {
            vec![]
        }
    }

    async fn authenticate_issued_identity_token(
        &self,
        _endpoint: &ServerEndpoint,
        token: &ByteString,
    ) -> Result<UserToken, Error> {
        let token_str =
            String::from_utf8(token.value.clone().unwrap_or_default()).map_err(Error::decoding)?;
        if token_str == "valid" {
            Ok(UserToken("valid".into()))
        } else {
            Err(Error::new(
                StatusCode::BadIdentityTokenRejected,
                "Invalid token",
            ))
        }
    }
}

#[tokio::test]
async fn issued_token_test() {
    let server = test_server()
        .add_endpoint(
            "issued_token",
            (
                "/issued_token",
                SecurityPolicy::Aes128Sha256RsaOaep,
                MessageSecurityMode::SignAndEncrypt,
                &[] as &[&str],
            ),
        )
        .with_authenticator(Arc::new(IssuedTokenAuthenticator));
    let mut tester = Tester::new(server, false).await;
    let (session, lp) = tester
        .connect_path(
            SecurityPolicy::Aes128Sha256RsaOaep,
            MessageSecurityMode::SignAndEncrypt,
            IdentityToken::IssuedToken(IssuedTokenWrapper::new_source(ByteString::from(
                "valid".as_bytes(),
            ))),
            "issued_token",
        )
        .await
        .unwrap();
    lp.spawn();
    tokio::time::timeout(Duration::from_secs(5), session.wait_for_connection())
        .await
        .unwrap();

    session
        .read(
            &[ReadValueId::from(<VariableId as Into<NodeId>>::into(
                VariableId::Server_ServiceLevel,
            ))],
            TimestampsToReturn::Both,
            0.0,
        )
        .await
        .unwrap();
}