Skip to content

Commit 12e0c49

Browse files
authored
Merge pull request #1290 from grafana/feat/grpc-docs
Document GPRC Connection's TLS option
2 parents f2577b4 + 213ce62 commit 12e0c49

File tree

2 files changed

+152
-4
lines changed

2 files changed

+152
-4
lines changed

src/data/markdown/docs/02 javascript api/07 k6-experimental/02 grpc/20 Client/20-Client-connect-connect-address-params.md

Lines changed: 76 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,20 @@ See [Client.close()](/javascript-api/k6-experimental/grpc/client/client-close) t
2020
| `ConnectParams.plaintext` | bool | If `true` will connect to the gRPC server using plaintext i.e. insecure. Defaults to `false` i.e. secure via TLS. |
2121
| `ConnectParams.reflect` | boolean | Whether to use the [gRPC server reflection protocol](https://github.com/grpc/grpc/blob/master/doc/server-reflection.md) when connecting. |
2222
| `ConnectParams.timeout` | string / number | Connection timeout to use. Default timeout is `"60s"`. <br/> The type can also be a number, in which case k6 interprets it as milliseconds, e.g., `60000` is equivalent to `"60s"`. |
23-
| `ConnectParams.maxReceiveSize` | number | Sets the maximum message size in bytes the client can receive.Defaults to 0. |
24-
| `ConnectParams.maxSendSize` | number | Sets the maximum message size in bytes the client can send.Defaults to 0. |
23+
| `ConnectParams.maxReceiveSize` | number | Sets the maximum message size in bytes the client can receive. Defaults to 0. |
24+
| `ConnectParams.maxSendSize` | number | Sets the maximum message size in bytes the client can send. Defaults to 0. |
25+
| `ConnectParams.tls` (optional) | object | [TLS](#tls) settings of the connection. Defaults to `null`. |
26+
27+
## TLS
28+
29+
TLS settings of the connection. If not defined, the main TLS config from options will be used.
30+
31+
| Name | Type | Description |
32+
|------|------|-------------|
33+
| `tls.cert` | string | PEM formatted client certificate. |
34+
| `tls.key` | string | PEM formatted client private key. |
35+
| `tls.password` | string | Password for decrypting the client's private key. |
36+
| `tls.cacerts` | string / array | PEM formatted strings of the certificate authorities. |
2537

2638
### Examples
2739

@@ -50,3 +62,65 @@ export default () => {
5062
};
5163
```
5264
</div>
65+
66+
<div class="code-group" data-props='{"labels": ["Different TLS settings"], "lineNumbers": [true]}'>
67+
68+
```javascript
69+
import grpc from "k6/experimental/grpc";
70+
import { check } from "k6";
71+
import { SharedArray } from "k6/data";
72+
import exec from "k6/execution";
73+
74+
// note: the services in this example don't exist. If you would like
75+
// to run this example, make sure to replace the URLs, and
76+
// the cacerts, cert, key, and password variables.
77+
const grpcArgs = new SharedArray("grpc", () => {
78+
// Using SharedArray here so that not every VU gets a copy of every certificate a key
79+
return [
80+
{
81+
host: "foo1.grpcbin.test.k6.io:9001",
82+
plaintext: false,
83+
params: {
84+
tls: {
85+
cacerts: [open("cacerts0.pem")],
86+
cert: open("cert0.pem"),
87+
key: open("key0.pem"),
88+
},
89+
},
90+
},
91+
{
92+
host: "foo2.grpcbin.test.k6.io:9002",
93+
params: {
94+
plaintext: false,
95+
tls: {
96+
cacerts: open("cacerts1.pem"),
97+
cert: open("cert1.pem"),
98+
key: open("key1.pem"),
99+
password: "cert1-passphrase",
100+
},
101+
},
102+
},
103+
];
104+
});
105+
106+
const client = new grpc.Client();
107+
108+
export default () => {
109+
if (__ITER === 0) {
110+
// Take one config and use it for this one VU
111+
const grpcArg = grpcArgs[exec.vu.idInTest % grpcArgs.length];
112+
client.connect(grpcArg.host, grpcArg.params);
113+
}
114+
115+
const response = client.invoke("hello.HelloService/SayHello", {
116+
greeting: "Bert",
117+
});
118+
119+
check(response, {
120+
"status is OK": (r) => r && r.status === grpc.StatusOK,
121+
});
122+
123+
console.log(JSON.stringify(response.message));
124+
};
125+
```
126+
</div>

src/data/markdown/docs/02 javascript api/11 k6-net-grpc/20 Client/20-Client-connect-connect-address-params.md

Lines changed: 76 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,20 @@ See [Client.close()](/javascript-api/k6-net-grpc/client/client-close) to close t
2020
| `ConnectParams.plaintext` | bool | If `true` will connect to the gRPC server using plaintext i.e. insecure. Defaults to `false` i.e. secure via TLS. |
2121
| `ConnectParams.reflect` | boolean | Whether to use the [gRPC server reflection protocol](https://github.com/grpc/grpc/blob/master/doc/server-reflection.md) when connecting. |
2222
| `ConnectParams.timeout` | string / number | Connection timeout to use. Default timeout is `"60s"`. <br/> The type can also be a number, in which case k6 interprets it as milliseconds, e.g., `60000` is equivalent to `"60s"`. |
23-
| `ConnectParams.maxReceiveSize` | number | Sets the maximum message size in bytes the client can receive.Defaults to 0. |
24-
| `ConnectParams.maxSendSize` | number | Sets the maximum message size in bytes the client can send.Defaults to 0. |
23+
| `ConnectParams.maxReceiveSize` | number | Sets the maximum message size in bytes the client can receive. Defaults to 0. |
24+
| `ConnectParams.maxSendSize` | number | Sets the maximum message size in bytes the client can send. Defaults to 0. |
25+
| `ConnectParams.tls` (optional) | object | [TLS](#tls) settings of the connection. Defaults to `null`. |
26+
27+
## TLS
28+
29+
TLS settings of the connection. If not defined, the main TLS config from options will be used.
30+
31+
| Name | Type | Description |
32+
|------|------|-------------|
33+
| `tls.cert` | string | PEM formatted client certificate. |
34+
| `tls.key` | string | PEM formatted client private key. |
35+
| `tls.password` | string | Password for decrypting the client's private key. |
36+
| `tls.cacerts` | string / array | PEM formatted strings of the certificate authorities. |
2537

2638
### Examples
2739

@@ -50,3 +62,65 @@ export default () => {
5062
};
5163
```
5264
</div>
65+
66+
<div class="code-group" data-props='{"labels": ["Different TLS settings"], "lineNumbers": [true]}'>
67+
68+
```javascript
69+
import grpc from "k6/experimental/grpc";
70+
import { check } from "k6";
71+
import { SharedArray } from "k6/data";
72+
import exec from "k6/execution";
73+
74+
// note: the services in this example don't exist. If you would like
75+
// to run this example, make sure to replace the URLs, and
76+
// the cacerts, cert, key, and password variables.
77+
const grpcArgs = new SharedArray("grpc", () => {
78+
// Using SharedArray here so that not every VU gets a copy of every certificate a key
79+
return [
80+
{
81+
host: "foo1.grpcbin.test.k6.io:9001",
82+
plaintext: false,
83+
params: {
84+
tls: {
85+
cacerts: [open("cacerts0.pem")],
86+
cert: open("cert0.pem"),
87+
key: open("key0.pem"),
88+
},
89+
},
90+
},
91+
{
92+
host: "foo2.grpcbin.test.k6.io:9002",
93+
params: {
94+
plaintext: false,
95+
tls: {
96+
cacerts: open("cacerts1.pem"),
97+
cert: open("cert1.pem"),
98+
key: open("key1.pem"),
99+
password: "cert1-passphrase",
100+
},
101+
},
102+
},
103+
];
104+
});
105+
106+
const client = new grpc.Client();
107+
108+
export default () => {
109+
if (__ITER === 0) {
110+
// Take one config and use it for this one VU
111+
const grpcArg = grpcArgs[exec.vu.idInTest % grpcArgs.length];
112+
client.connect(grpcArg.host, grpcArg.params);
113+
}
114+
115+
const response = client.invoke("hello.HelloService/SayHello", {
116+
greeting: "Bert",
117+
});
118+
119+
check(response, {
120+
"status is OK": (r) => r && r.status === grpc.StatusOK,
121+
});
122+
123+
console.log(JSON.stringify(response.message));
124+
};
125+
```
126+
</div>

0 commit comments

Comments
 (0)