Skip to content

Commit c2c5ca4

Browse files
committed
Add documents for how to create a client config from JSON file
- Add a code snippet that demonstrates how to create a nosqldb.config from a JSON configuration file. - Clarify the "password" field specified in JSON config file must be a base64 encoding.
1 parent e7d4dde commit c2c5ca4

File tree

1 file changed

+73
-3
lines changed

1 file changed

+73
-3
lines changed

doc/connect.md

Lines changed: 73 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ client, err := nosqldb.NewClient(cfg)
321321

322322
To connect an application to a secure NoSQL database, you need to provide user
323323
credentials used to authenticate with the server. If the Proxy server is configured
324-
with a self-signed certificate or a certificate that is not trusted by the
324+
with a self-signed certificate or a certificate that is not trusted by
325325
the default system CA, you also need to specifiy *CertPath* and *ServerName* for
326326
the certificate path and server name used to verify server's certificate.
327327

@@ -330,7 +330,7 @@ cfg := nosqldb.Config{
330330
Mode: "onprem",
331331
Endpoint: "https://exampleHostServer",
332332
Username: "driverUser",
333-
Password: "ExamplePassword__123",
333+
Password: []byte("ExamplePassword__123"),
334334
HTTPConfig: httputil.HTTPConfig{
335335
CertPath: "/path/to/server-certificate",
336336
ServerName: "exampleHostServer", // should match the CN subject value from the certificate
@@ -348,10 +348,80 @@ cfg := nosqldb.Config{
348348
Mode: "onprem",
349349
Endpoint: "https://exampleHostServer",
350350
Username: "driverUser",
351-
Password: "ExamplePassword__123",
351+
Password: []byte("ExamplePassword__123"),
352352
HTTPConfig: httputil.HTTPConfig{
353353
InsecureSkipVerify: true,
354354
},
355355
}
356356
client, err := nosqldb.NewClient(cfg)
357357
```
358+
359+
#### Initialize configuration using a JSON file
360+
The sections above describe how to create a `nosqldb.Config` object using Go's
361+
struct literals, as an alternative, you can create and initialize a
362+
`nosqldb.Config` object using a JSON file. Note that for the fields of byte
363+
slice type, such as the `Password` field required for secure on-premise Oracle
364+
NoSQL Database, you need to specify a base64 encoded string in JSON file.
365+
For example, when connect to a secure on-premise Oracle NoSQL Database with
366+
user credentials *driver_user/DriverUser__123456*, where the username is
367+
*driver_user*, password is *DriverUser__123456*, base64 encoding of the
368+
password is *RHJpdmVyVXNlcl9fMTIzNDU2* (`echo -n "DriverUser__123456" | base64`),
369+
use a sample JSON file *config.json*:
370+
371+
```json
372+
{
373+
"mode": "onprem",
374+
"endpoint": "https://localhost:8091",
375+
"username": "driver_user",
376+
"password": "RHJpdmVyVXNlcl9fMTIzNDU2",
377+
"httpConfig": {
378+
"certPath": "/path/to/server_certificate.pem",
379+
"serverName": "localhost"
380+
}
381+
}
382+
```
383+
384+
Then create and initialize a `nosqldb.Config` object using the JSON file:
385+
```go
386+
package main
387+
388+
import (
389+
"encoding/json"
390+
"fmt"
391+
"io/ioutil"
392+
393+
"github.com/oracle/nosql-go-sdk/nosqldb"
394+
)
395+
396+
func createConfigFromJSON(configFile string) (*nosqldb.Config, error) {
397+
data, err := ioutil.ReadFile(configFile)
398+
if err != nil {
399+
return nil, fmt.Errorf("failed to read config file %s: %v", configFile, err)
400+
}
401+
402+
var cfg nosqldb.Config
403+
err = json.Unmarshal(data, &cfg)
404+
if err != nil {
405+
return nil, fmt.Errorf("failed to parse configurations from file %s: %v", configFile, err)
406+
}
407+
408+
return &cfg, nil
409+
}
410+
411+
func main() {
412+
413+
cfg, err := createConfigFromJSON("/path/to/config.json")
414+
if err != nil {
415+
fmt.Printf("failed to create config: %v", err)
416+
return
417+
}
418+
419+
client, err := nosqldb.NewClient(*cfg)
420+
if err != nil {
421+
fmt.Printf("failed to create client: %v", err)
422+
return
423+
}
424+
defer client.Close()
425+
...
426+
}
427+
```

0 commit comments

Comments
 (0)