RESTCONF API
Description of the RESTCONF API.
RESTCONF is an HTTP-based protocol as defined in RFC 8040. RESTCONF standardizes a mechanism to allow Web applications to access the configuration data, state data, data-model-specific Remote Procedure Call (RPC) operations, and event notifications within a networking device.
RESTCONF uses HTTP methods to provide Create, Read, Update, Delete (CRUD) operations on a conceptual datastore containing YANG-defined data, which is compatible with a server that implements NETCONF datastores as defined in RFC 6241.
Configuration data and state data are exposed as resources that can be retrieved with the GET method. Resources representing configuration data can be modified with the DELETE, PATCH, POST, and PUT methods. Data is encoded with either XML (W3C.REC-xml-20081126) or JSON (RFC 7159)
This chapter describes the NSO implementation and extension to or deviation from RFC 8040 respectively.
As of this writing, the server supports the following specifications:
RFC 6020 - YANG - A Data Modeling Language for the Network Configuration Protocol (NETCONF)
RFC 6021 - Common YANG Data Types
RFC 6470 - NETCONF Base Notifications
RFC 6536 - NETCONF Access Control Model
RFC 6991 - Common YANG Data Types
RFC 7950 - The YANG 1.1 Data Modeling Language
RFC 7951 - JSON Encoding of Data Modeled with YANG
RFC 7952 - Defining and Using Metadata with YANG
RFC 8040 - RESTCONF Protocol
RFC 8072 - YANG Patch Media Type
RFC 8341 - Network Configuration Access Control Model
RFC 8525 - YANG Library
RFC 8528 - YANG Schema Mount
Getting Started
To enable RESTCONF in NSO, RESTCONF must be enabled in the ncs.conf
configuration file. The web server configuration for RESTCONF is shared with the WebUI's config, but you may define a separate RESTCONF transport section. The WebUI does not have to be enabled for RESTCONF to work.
Here is a minimal example of what is needed in the ncs.conf
.
If you want to run RESTCONF with a different transport configuration than what the WebUI is using, you can specify a separate RESTCONF transport section.
It is now possible to do a RESTCONF requests towards NSO. Any HTTP client can be used, in the following examples curl will be used. The example below will show what a typical RESTCONF request could look like.
In the rest of the document, in order to simplify the presentation, the example above will be expressed as:
Note the HTTP return code (200 OK) in the example, which will be displayed together with any relevant HTTP headers returned and a possible body of content.
Top-level GET request
Send a RESTCONF query to get a representation of the top-level resource, which is accessible through the path: /restconf
.
As can be seen from the result, the server exposes three additional resources:
data
: This mandatory resource represents the combined configuration and state data resources that can be accessed by a client.operations
: This optional resource is a container that provides access to the data-model-specific RPC operations supported by the server.yang-library-version
: This mandatory leaf identifies the revision date of theietf-yang-library
YANG module that is implemented by this server. This resource exposes which YANG modules are in use by the NSO system.
Get Resources Under the data
Resource
data
ResourceTo fetch configuration, operational data, or both, from the server, a request to the data
resource is made. To restrict the amount of returned data, the following example will prune the amount of output to only consist of the topmost nodes. This is achieved by using the depth
query argument as shown in the example below:
Manipulating config data with RESTCONF
Let's assume we are interested in the dhcp/subnet
resource in our configuration. In the following examples, assume that it is defined by a corresponding Yang module that we have named dhcp.yang
, looking like this:
We can issue an HTTP GET request to retrieve the value content of the resource. In this case, we find that there is no such data, which is indicated by the HTTP return code 204 No Content
.
Note also how we have prefixed the dhcp:dhcp
resource. This is how RESTCONF handles namespaces, where the prefix is the YANG module name and the namespace is as defined by the namespace statement in the YANG module.
We can now create the dhcp/subnet
resource by sending an HTTP POST request + the data that we want to store. Note the Content-Type
HTTP header, which indicates the format of the provided body. Two formats are supported: XML or JSON. In this example, we are using XML, which is indicated by the Content-Type
value: application/yang-data+xml
.
Note the HTTP return code (201 Created
) indicating that the resource was successfully created. We also got a Location header, which always is returned in a reply to a successful creation of a resource, stating the resulting URI leading to the created resource.
If we now want to modify a part of our dhcp/subnet
config, we can use the HTTP PATCH
method, as shown below. Note that the URI used in the request needs to be URL-encoded, such that the key value: 10.254.239.0/27
is URL-encoded as: 10.254.239.0%2F27
.
Also, note the difference of the PATCH
URI compared to the earlier POST
request. With the latter, since the resource does not yet exist, we POST
to the parent resource (dhcp:dhcp
), while with the PATCH
request we address the (existing) resource (10.254.239.0%2F27
).
We can also replace the subnet with some new configuration. To do this, we make use of the PUT
HTTP method as shown below. Since the operation was successful and no body was returned, we will get a 204 No Content
return code.
To delete the subnet, we make use of the DELETE
HTTP method as shown below. Since the operation was successful and no body was returned, we will get a 204 No Content
return code.
Root Resource Discovery
RESTCONF makes it possible to specify where the RESTCONF API is located, as described in the RESTCONF RFC 8040.
As per default, the RESTCONF API root is /restconf
. Typically there is no need to change the default value although it is possible to change this by configuring the RESTCONF API root in the ncs.conf
file as:
The RESTCONF API root will now be /my_own_restconf_root
.
A client may discover the root resource by getting the /.well-known/host-meta
resource as shown in the example below:
In this guide, all examples will assume the RESTCONF API root to be /restconf
.
Capabilities
A RESTCONF capability is a set of functionality that supplements the base RESTCONF specification. The capability is identified by a uniform resource identifier (URI). The RESTCONF server includes a capability
URI leaf-list entry identifying each supported protocol feature. This includes the basic-mode
default-handling mode, optional query parameters, and may also include other, NSO-specific, capability URIs.
How to View the Capabilities of the RESTCONF Server
To view currently enabled capabilities, use the ietf-restconf-monitoring
YANG model, which is available as: /restconf/data/ietf-restconf-monitoring:restconf-state
.
The defaults
Capability
defaults
CapabilityThis Capability identifies the basic-mode
default-handling mode that is used by the server for processing default leafs in requests for data resources.
The capability
URL will contain a query parameter named basic-mode
which value tells us what the default behavior of the RESTCONF server is when it returns a leaf. The possible values are shown in the table below (basic-mode
values):
The values presented in the table above can also be used by the Client together with the with-defaults
query parameter to override the default RESTCONF server behavior. Added to these values, the Client can also use the report-all-tagged
value.
The table below lists additional with-defaults
value.
Referring back to the example: Example: NSO RESTCONF Capabilities, where the RESTCONF server returned the default capability:
It tells us that values that have been set by a client to the YANG default value will be reported but default values that have not been set by the Client will not be returned. Again, note that this is the default RESTCONF server behavior which can be overridden by the Client by using the with-defaults
query argument.
Query Parameter Capabilities
A set of optional RESTCONF Capability URIs are defined to identify the specific query parameters that are supported by the server. They are defined as:
The table shows query parameter capabilities.
For a description of the query parameter functionality, see Query Parameters.
Query Parameters
Each RESTCONF operation allows zero or more query parameters to be present in the request URI. Query parameters can be given in any order, but can appear at most once. Supplying query parameters when invoking RPCs and actions is not supported, if supplied the response will be 400 (Bad Request) and the error-app-tag
will be set to invalid-value
. However, the query parameters trace-id
and unhide
are exempted from this rule and supported for RPC and action invocation. The defined query parameters and in what type of HTTP request they can be used are shown in the table below (Query parameters).
The content
Query Parameter
content
Query ParameterThe content
query parameter controls if configuration, non-configuration, or both types of data should be returned. The content
query parameter values are listed below.
The allowed values are:
The depth
Query Parameter
depth
Query ParameterThe depth
query parameter is used to limit the depth of subtrees returned by the server. Data nodes with a value greater than the depth
parameter are not returned in response to a GET request.
The value of the depth
parameter is either an integer between 1 and 65535 or the string unbounded
. The default value is: unbounded
.
The fields
Query Parameter
fields
Query ParameterThe fields
query parameter is used to optionally identify data nodes within the target resource to be retrieved in a GET method. The client can use this parameter to retrieve a subset of all nodes in a resource.
For a full definition of the fields
value can be constructed, refer to the RFC 8040, Section 4.8.3.
Note that the fields
query parameter cannot be used together with the exclude
query parameter. This will result in an error.
The exclude
Query Parameter
exclude
Query ParameterThe exclude
query parameter is used to optionally exclude data nodes within the target resource from being retrieved with a GET request. The client can use this parameter to exclude a subset of all nodes in a resource. Only nodes below the target resource can be excluded, not the target resource itself.
Note that the exclude
query parameter cannot be used together with the fields
query parameter. This will result in an error.
The exclude
query parameter uses the same syntax and has the same restrictions as the fields
query parameter, as defined in RFC 8040, Section 4.8.3.
Selecting multiple nodes to exclude can be done the same way as for the fields
query parameter, as described in RFC 8040, Section 4.8.3.
exclude
using wildcards (*) will exclude all child nodes of the node. For lists and presence containers, the parent node will be visible in the output but not its children, i.e. it will be displayed as an empty node. For non-presence containers, the parent node will be excluded from the output as well.
exclude
can be used together with the depth
query parameter to limit the depth of the output. In contrast to fields
, where depth
is counted from the node selected by fields
, for exclude
the depth is counted from the target resource, and the nodes are excluded if depth
is deep enough to encounter an excluded node.
When exclude
is not used:
Using exclude
to exclude low
and high
from range
, note that these are absent in the output:
The filter
, start-time
, and stop-time
Query Parameters.
filter
, start-time
, and stop-time
Query Parameters.These query parameters are only allowed on an event stream resource and are further described in Streams.
The insert
Query Parameter
insert
Query ParameterThe insert
query parameter is used to specify how a resource should be inserted within an ordered-by user
list. The allowed values are shown in the table below (The content
query parameter values).
This parameter is only valid if the target data represents a YANG list or leaf-list that is ordered-by user
. In the example below, we will insert a new router
value, first, in the ordered-by user
leaf-list of dhcp-options/router
values. Remember that the default behavior is for new entries to be inserted last in an ordered-by user
leaf-list.
To verify that the router
value really ended up first:
The point
Query Parameter
point
Query ParameterThe point
query parameter is used to specify the insertion point for a data resource that is being created or moved within an ordered-by user
list or leaf-list. In the example below, we will insert the new router
value: two.acme.org
, after the first value: one.acme.org
in the ordered-by user
leaf-list of dhcp-options/router
values.
To verify that the router
value really ended up after our insertion point:
Additional Query Parameters
There are additional NSO query parameters available for the RESTCONF API. These additional query parameters are described in the table below (Additional Query Parameters).
Edit Collision Prevention
Two edit collision detection and prevention mechanisms are provided in RESTCONF for the datastore resource: a timestamp and an entity tag. Any change to configuration data resources will update the timestamp and entity tag of the datastore resource. This makes it possible for a client to apply precondition HTTP headers to a request.
The NSO RESTCONF API honors the following HTTP response headers: Etag
and Last-Modified
, and the following request headers: If-Match
, If-None-Match
, If-Modified-Since
, and If-Unmodified-Since
.
Response Headers
Etag
: This header will contain an entity tag which is an opaque string representing the latest transaction identifier in the NSO database. This header is only available for the running datastore and hence, only relates to configuration data (non-operational).Last-Modified
: This header contains the timestamp for the last modification made to the NSO database. This timestamp can be used by a RESTCONF client in subsequent requests, within theIf-Modified-Since
andIf-Unmodified-Since
header fields. This header is only available for the running datastore and hence, only relates to configuration data (non-operational).
Request Headers
If-None-Match
: This header evaluates to true if the supplied value does not match the latestEtag
entity-tag value. If evaluated to false, an error response with status 304 (Not Modified) will be sent with no body. This header carries only meaning if the entity tag of theEtag
response header has previously been acquired. The usage of this could for example be a HEAD operation to get information if the data has changed since the last retrieval.If-Modified-Since
: This request-header field is used with an HTTP method to make it conditional, i.e if the requested resource has not been modified since the time specified in this field, the request will not be processed by the RESTCONF server; instead, a 304 (Not Modified) response will be returned without any message-body. Usage of this is for instance for a GET operation to retrieve the information if (and only if) the data has changed since the last retrieval. Thus, this header should use the value of aLast-Modified
response header that has previously been acquired.If-Match
: This header evaluates to true if the supplied value matches the latestEtag
value. If evaluated to false, an error response with status 412 (Precondition Failed) will be sent with no body. This header carries only meaning if the entity tag of theEtag
response header has previously been acquired. The usage of this can be in the case of aPUT
, whereIf-Match
can be used to prevent the lost update problem. It can check if the modification of a resource that the user wants to upload will not override another change that has been done since the original resource was fetched.If-Unmodified-Since
: This header evaluates to true if the supplied value has not been last modified after the given date. If the resource has been modified after the given date, the response will be a 412 (Precondition Failed) error with no body. This header carries only meaning if theLast-Modified
response header has previously been acquired. The usage of this can be the case of aPOST
, where editions are rejected if the stored resource has been modified since the original value was retrieved.
Using Rollbacks
Rolling Back Configuration Changes
If rollbacks have been enabled in the configuration using the rollback-id
query parameter, the fixed ID of the rollback file created during an operation is returned in the results. The below examples show the creation of a new resource and the removal of that resource using the rollback created in the first step.
Then using the fixed ID returned above as input to the apply-rollback-file
action:
Streams
Introduction
The RESTCONF protocol supports YANG-defined event notifications. The solution preserves aspects of NETCONF event notifications [RFC5277] while utilizing the Server-Sent Events, W3C.REC-eventsource-20150203, transport strategy.
RESTCONF event notification streams are described in Sections 6 and 9.2 of RFC 8040, where also notification examples can be found.
RESTCONF event notification is a way for RESTCONF clients to retrieve notifications for different event streams. Event streams configured in NSO can be subscribed to using different channels such as the RESTCONF or the NETCONF channel.
More information on how to define a new notification event using Yang is described in RFC 6020.
How to add and configure notifications support in NSO is described in the ncs.conf(3)
man page.
The design of RESTCONF event notification is inspired by how NETCONF event notification is designed. More information on NETCONF event notification can be found in RFC 5277.
Configuration
For this example, we will define a notification stream, named interface
in the ncs.conf
configuration file as shown below.
We also enable the built-in replay store which means that NSO automatically stores all notifications on disk, ready to be replayed should a RESTCONF event notification subscriber ask for logged notifications. The replay store uses a set of wrapping log files on a disk (of a certain number and size) to store the notifications.
To view the currently enabled event streams, use the ietf-restconf-monitoring
YANG model. The streams are available under the /restconf/data/ietf-restconf-monitoring:restconf-state/streams
container.
Note the URL value we get in the location element in the example above. This URL should be used when subscribing to the notification events as is shown in the next example.
Subscribe to Notification Events
RESTCONF clients can determine the URL for the subscription resource (to receive notifications) by sending an HTTP GET request for the location
leaf with the stream
list entry. The value returned by the server can be used for the actual notification subscription.
The client will send an HTTP GET request for the (location) URL returned by the server with the Accept
type text/event-stream
as shown in the example below. Note that this request works like a long polling request which means that the request will not return. Instead, server-side notifications will be sent to the client where each line of the notification will be prepended with data:
.
Since we have enabled the replay store, we can ask the server to replay any notifications generated since the specific date we specify. After those notifications have been delivered, we will continue waiting for new notifications to be generated.
Errors
Errors occurring during streaming of events will be reported as Server-Sent Events (SSE) comments as described in W3C.REC-eventsource-20150203 as shown in the example below.
Schema Resource
RFC 8040, Section 3.7 describes the retrieval of YANG modules used by the server via the RPC operation get-schema
. The YANG source is made available by NSO in two ways: compiled into the fxs
file or put in the loadPath. See Monitoring of the NETCONF Server.
The example below shows how to list the available Yang modules. Since we are interested in the dhcp
module, we only show that part of the output:
We can now retrieve the dhcp
Yang module via the URL we got in the schema
leaf of the reply. Note that the actual URL may point anywhere. The URL is configured by the schemaServerUrl
setting in the ncs.conf
file.
YANG Patch Media Type
The NSO RESTCONF API also supports the YANG Patch Media Type, as defined in RFC 8072.
A YANG Patch
is an ordered list of edits that are applied to the target datastore by the RESTCONF server. A YANG Patch request is sent as an HTTP PATCH request containing a body describing the edit operations to be performed. The format of the body is defined in the RFC 8072.
Referring to the example above (dhcp Yang model) in the Getting Started section; we will show how to use YANG Patch to achieve the same result but with fewer amount of requests.
Create Two New Resources with the YANG Patch
To create the resources, we send an HTTP PATCH request where the Content-Type
indicates that the body in the request consists of a Yang-Patch
message. Our Yang-Patch
request will initiate two edit operations where each operation will create a new subnet. In contrast, compare this with using plain RESTCONF where we would have needed two POST
requests to achieve the same result.
Modify and Delete in the Same Yang-Patch Request
Let us modify the max-lease-time
of one subnet and delete the max-lease-time
value of the second subnet. Note that the delete will cause the default value of max-lease-time
to take effect, which we will verify using a RESTCONF GET request.
To verify that our modify and delete operations took place we make use of two RESTCONF GET
requests as shown below.
Note how we in the last GET
request make use of the with-defaults
query parameter to request that a default value should be returned and also be tagged as such.
NMDA
Network Management Datastore Architecture (NMDA), as defined in RFC 8527, extends the RESTCONF protocol. This enables RESTCONF clients to discover which datastores are supported by the RESTCONF server, determine which modules are supported in each datastore, and interact with all the datastores supported by the NMDA.
A RESTCONF client can test if a server supports the NMDA by using either the HEAD
or GET
methods on /restconf/ds/ietf- datastores:operational
, as shown below:
A RESTCONF client can discover which datastores and YANG modules the server supports by reading the YANG library information from the operational state datastore. Note in the example below that, since the result consists of three top nodes, it can't be represented in XML; hence we request the returned content to be in JSON format. See also Collections.
Extensions
To avoid any potential future conflict with the RESTCONF standard, any extensions made to the NSO implementation of RESTCONF are located under the URL path: /restconf/tailf
, or is controlled by means of a vendor-specific media type.
There is no index of extensions under /restconf/tailf
. To list extensions, access /restconf/data/ietf-yang-library:modules-state
and follow published links for schemas.
Collections
The RESTCONF specification states that a result containing multiple instances (e.g. a number of list entries) is not allowed if XML encoding is used. The reason for this is that an XML document can only have one root node.
This functionality is supported if the http://tail-f.com/ns/restconf/collection/1.0
capability is presented. See also How to View the Capabilities of the RESTCONF Server.
To remedy this, an HTTP GET request can make use of the Accept:
media type: application/vnd.yang.collection+xml
as shown in the following example. The result will then be wrapped within a collection
element.
The RESTCONF Query API
The NSO RESTCONF Query API consists of a number of operations to start a query which may live over several RESTCONF requests, where data can be fetched in suitable chunks. The data to be returned is produced by applying an XPath expression where the data also may be sorted.
The RESTCONF client can check if the NSO RESTCONF server supports this functionality by looking for the http://tail-f.com/ns/restconf/query-api/1.0
capability. See also How to View the Capabilities of the RESTCONF Server.
The tailf-rest-query.yang
and the tailf-common-query.yang
YANG models describe the structure of the RESTCONF Query API messages. By using the Schema Resource functionality, as described in Schema Resource, you can get hold of them.
Request and Replies
The API consists of the following requests:
start-query
: Start a query and return a query handle.fetch-query-result
: Use a query handle to repeatedly fetch chunks of the result.immediate-query
: Start a query and return the entire result immediately.reset-query
: (Re)set where the next fetched result will begin from.stop-query
: Stop (and close) the query.
The API consists of the following replies:
start-query-result
: Reply to the start-query request.query-result
: Reply to the fetch-query-result and immediate-query requests.
In the following examples, we'll use this data model:
The actual format of the payload should be represented either in XML or JSON. Note how we indicate the type of content using the Content-Type
HTTP header. For XML, it could look like this:
The same request in JSON format would look like:
An informal interpretation of this query is:
For each /x/host
where enabled
is true, select its name
, and address
, and return the result sorted by name
, in chunks of 100 result items at a time.
Let us discuss the various pieces of this request. To start with, when using XML, we need to specify the namespace as shown:
The actual XPath query to run is specified by the foreach
element. The example below will search for all /x/host
nodes that have the enabled
node set to true
:
Now we need to define what we want to have returned from the node set by using one or more select
sections. What to actually return is defined by the XPath expression
.
Choose how the result should be represented. Basically, it can be the actual value or the path leading to the value. This is specified per select chunk. The possible result types are string
, path
, leaf-value
, and inline
.
The difference between string
and leaf-value
is somewhat subtle. In the case of string
, the result will be processed by the XPath function: string()
(which if the result is a node-set will concatenate all the values). The leaf-value
will return the value of the first node in the result. As long as the result is a leaf node, string
and leaf-value
will return the same result. In the example above, the string
is used as shown below. Note that at least one result-type
must be specified.
The result-type inline
makes it possible to return the full sub-tree of data, either in XML or in JSON format. The data will be enclosed with a tag: data
.
It is possible to specify an optional label
for a convenient way of labeling the returned data:
The returned result can be sorted. This is expressed as an XPath expression, which in most cases is very simple and refers to the found node-set. In this example, we sort the result by the content of the name node:
With the offset
element, we can specify at which node we should start to receive the result. The default is 1, i.e., the first node in the resulting node set.
It is possible to set a custom timeout when starting or resetting a query. Each time a function is called, the timeout timer resets. The default is 600 seconds, i.e. 10 minutes.
The reply to this request would look something like this:
The query handle (in this example '12345') must be used in all subsequent calls. To retrieve the result, we can now send:
Which will result in something like the following:
If we try to get more data with the fetch-query-result
, we might get more result
entries in return until no more data exists and we get an empty query result back:
Finally, when we are done we stop the query:
Reset a Query
If we want to go back into the stream of received data chunks and have them repeated, we can do that with the reset-query
request. In the example below, we ask to get results from the 42nd result entry:
Immediate Query
If we want to get the entire result sent back to us, using only one request, we can do this by using the immediate-query
. This function takes similar arguments as start-query
and returns the entire result analogous with the result from a fetch-query-result
request. Note that it is not possible to paginate or set an offset start node for the result list; i.e. the options limit
and offset
are ignored.
Partial Responses
This functionality is supported if the http://tail-f.com/ns/restconf/partial-response/1.0
capability is presented. See also How to View the Capabilities of the RESTCONF Server.
By default, the server sends back the full representation of a resource after processing a request. For better performance, the server can be instructed to send only the nodes the client really needs in a partial response.
To request a partial response for a set of list entries, use the offset
and limit
query parameters to specify a limited set of entries to be returned.
In the following example, we retrieve only two entries, skipping the first entry and then returning the next two entries:
Hidden Nodes
This functionality is supported if the http://tail-f.com/ns/restconf/unhide/1.0
capability is presented. See also How to View the Capabilities of the RESTCONF Server.
By default, hidden nodes are not visible in the RESTCONF interface. To unhide hidden nodes for retrieval or editing, clients can use the query parameter unhide
or set parameter showHidden
to true
under /confdConfig/restconf
in confd.conf
file. The query parameter unhide
is supported for RPC and action invocation.
The format of the unhide
parameter is a comma-separated list of
As an example:
This example unhides the unprotected group extra and the password-protected group debug
with the password secret;
.
Configuration Metadata
It is possible to associate metadata with the configuration data. For RESTCONF, resources such as containers, lists as well as leafs and leaf-lists can have such meta-data. For XML, this meta-data is represented as attributes attached to the XML element in question. For JSON, there does not exist a natural way to represent this info. Hence a special special notation has been introduced, based on the RFC 7952, see the example below.
For JSON, note how we represent the metadata for a certain object "x"
by another object constructed of the object name prefixed with either one or two "@"
signs. The meta-data object "@x"
refers to the sibling object "x"
and the "@@x"
object refers to the parent object.
This differs from the RFC 7952.
Authentication Cache
The RESTCONF server maintains an authentication cache. When authenticating an incoming request for a particular User:Password
, it is first checked if the User exists in the cache and if so, the request is processed. This makes it possible to avoid the, potentially time-consuming, login procedure that will take place in case of a cache miss.
Cache entries have a maximum Time-To-Live (TTL) and upon expiry, a cache entry is removed which will cause the next request for that User to perform the normal login procedure. The TTL value is configurable via the auth-cache-ttl
parameter, as shown in the example. Note that, by setting the TTL value to PT0S
(zero), the cache is effectively turned off.
It is also possible to combine the Client's IP address with the User name as a key into the cache. This behavior is disabled by default. It can be enabled by setting the enable-auth-cache-client-ip
parameter to true
. With this enabled, only a Client coming from the same IP address may get a hit in the authentication cache.
Client IP via Proxy
It is possible to configure the NSO RESTCONF server to pick up the client IP address via an HTTP header in the request. A list of HTTP headers to look for is configurable via the proxy-headers
parameter as shown in the example.
To avoid misuse of this feature, only requests from trusted sources will be searched for such an HTTP header. The list of trusted sources is configured via the allowed-proxy-ip-prefix
as shown in the example.
External Token Authentication/Validation
The NSO RESTCONF server can be set up to pass a long, a token used for authentication and/or validation of the client. Note that this requires external authentication/validation
to be set up properly. See External Token Validation and External Authentication for details.
With token authentication, we mean that the client sends a User:Password
to the RESTCONF server, which will invoke an external executable that performs the authentication and upon success produces a token that the RESTCONF server will return in the X-Auth-Token
HTTP header of the reply.
With token validation, we mean that the RESTCONF server will pass along any token, provided in the X-Auth-Token
HTTP header, to an external executable that performs the validation. This external program may produce a new token that the RESTCONF server will return in the X-Auth-Token
HTTP header of the reply.
To make this work, the following need to be configured in the ncs.conf
file:
It is also possible to have the RESTCONF server to return a HTTP cookie containing the token.
An HTTP cookie (web cookie, browser cookie) is a small piece of data that a server sends to the user's web browser. The browser may store it and send it back with the next request to the same server. This can be convenient in certain solutions, where typically, it is used to tell if two requests came from the same browser, keeping a user logged in, for example.
To make this happen, the name of the cookie needs to be configured as well as a directives
string which will be sent as part of the cookie.
Custom Response HTTP Headers
The RESTCONF server can be configured to reply with particular HTTP headers in the HTTP response. For example, to support Cross-Origin Resource Sharing (CORS, https://www.w3.org/TR/cors/) there is a need to add a couple of headers to the HTTP Response.
We add the extra configuration parameter in ncs.conf
.
A number of HTTP headers have been deemed so important by security reasons that they, with sensible default values, per default will be included in the RESTCONF reply. The values can be changed by configuration in the ncs.conf
file. Note that a configured empty value will effectively turn off that particular header from being included in the RESTCONF reply. The headers and their default values are:
xFrameOptions
:DENY
The default value indicates that the page cannot be displayed in a frame/iframe/embed/object regardless of the site attempting to do so.
xContentTypeOptions
:nosniff
The default value indicates that the MIME types advertised in the Content-Type headers should not be changed and be followed. In particular, should requests for CSS or Javascript be blocked in case a proper MIME type is not used.
xXssProtection
:1; mode=block
This header is a feature of Internet Explorer, Chrome and Safari that stops pages from loading when they detect reflected cross-site scripting (XSS) attacks. It enables XSS filtering and tells the browser to prevent rendering of the page if an attack is detected.
strictTransportSecurity
:max-age=15552000; includeSubDomains
The default value tells browsers that the RESTCONF server should only be accessed using HTTPS, instead of using HTTP. It sets the time that the browser should remember this and states that this rule applies to all of the server's subdomains as well.
contentSecurityPolicy
:default-src 'self'; block-all-mixed-content; base-uri 'self'; frame-ancestors 'none';
The default value means that: Resources like fonts, scripts, connections, images, and styles will all only load from the same origin as the protected resource. All mixed contents will be blocked and frame-ancestors like iframes and applets are prohibited.
Generating Swagger for RESTCONF
Swagger is a documentation language used to describe RESTful APIs. The resulting specifications are used to both document APIs as well as generating clients in a variety of languages. For more information about the Swagger specification itself and the ecosystem of tools available for it, see swagger.io.
The RESTCONF API in NSO provides an HTTP-based interface for accessing data. The YANG modules loaded into the system define the schema for the data structures that can be manipulated using the RESTCONF protocol. The yanger
tool provides options to generate Swagger specifications from YANG files. The tool currently supports generating specifications according to OpenAPI/Swagger 2.0 using JSON encoding. The tool supports the validation of JSON bodies in body parameters and response bodies, and XML content validation is not supported.
YANG and Swagger are two different languages serving slightly different purposes. YANG is a data modeling language used to model configuration data, state data, Remote Procedure Calls, and notifications for network management protocols such as NETCONF and RESTCONF. Swagger is an API definition language that documents API resource structure as well as HTTP body content validation for applicable HTTP request methods. Translation from YANG to Swagger is not perfect in the sense that there are certain constructs and features in YANG that is not possible to capture completely in Swagger. The design of the translation is designed such that the resulting Swagger definitions are more restrictive than what is expressed in the YANG definitions. This means that there are certain cases where a client can do more in the RESTCONF API than what the Swagger definition expresses. There is also a set of well-known resources defined in the RESTCONF RFC 8040 that are not part of the generated Swagger specification, notably resources related to event streams.
Using Yanger to Generate Swagger
The yanger
tool is a YANG parser and validator that provides options to convert YANG modules to a multitude of formats including Swagger. You use the -f swagger
option to generate a Swagger definition from one or more YANG files. The following command generates a Swagger file named example.json
from the example.yang
YANG file:
It is only supported to generate Swagger from one YANG module at a time. It is possible however to augment this module by supplying additional modules. The following command generates a Swagger document from base.yang
which is augmented by base-ext-1.yang
and base-ext-2.yang
:
Only supplying augmenting modules is not supported.
Use the --help
option to the yanger
command to see all available options:
The complete list of options related to Swagger generation is:
Using the example-jukebox.yang
from the RESTCONF RFC 8040, the following example generates a comprehensive Swagger definition using a variety of Swagger-related options:
Last updated