Skip to content

Commit

Permalink
plugin/clnrest: Adding new config param as clnrest-swagger-root
Browse files Browse the repository at this point in the history
- Updated config params and plugin
- Updated documentation

Changelog-Added: Added a new configuration for clnrest plugin to change the default Swagger UI path from `/` to custom url.
  • Loading branch information
ShahanaFarooqui committed Apr 16, 2024
1 parent c4edec8 commit 6b98949
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 4 deletions.
2 changes: 2 additions & 0 deletions doc/developers-guide/app-development/rest.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ clnrest-cors-origins=https?://127.0.0.1:([0-9]{1,4}|[1-5][0-9]{4}|6[0-4][0-9]{3}
```

- --clnrest-swagger-root: Root url for Swagger UI. Default is `/`. Example: `clnrest-swagger-root=/doc`

## Server

With the default configurations, the Swagger user interface will be available at https://127.0.0.1:3010/.
Expand Down
4 changes: 4 additions & 0 deletions doc/lightningd-config.5.md
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,10 @@ authenticate to the Tor control port.

Creates a whitelist of trusted content sources that can run on a webpage and helps mitigate the risk of attacks. Default CSP is `default-src 'self'; font-src 'self'; img-src 'self' data:; frame-src 'self'; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline';`.

* **clnrest-swagger-root**=*URL* [plugin `clnrest.py`]

Root url for Swagger UI. Default is `/`.

### Lightning Plugins

lightningd(8) supports plugins, which offer additional configuration
Expand Down
4 changes: 2 additions & 2 deletions plugins/clnrest/clnrest.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,15 @@ def ws_connect():


def create_app():
from utilities.shared import REST_CORS_ORIGINS
from utilities.shared import REST_CORS_ORIGINS, SWAGGER_ROOT
global app
app.config["SECRET_KEY"] = os.urandom(24).hex()
authorizations = {
"rune": {"type": "apiKey", "in": "header", "name": "Rune"}
}
CORS(app, resources={r"/*": {"origins": REST_CORS_ORIGINS}})
blueprint = Blueprint("api", __name__)
api = Api(blueprint, version="1.0", title="Core Lightning Rest", description="Core Lightning REST API Swagger", authorizations=authorizations, security=["rune"])
api = Api(blueprint, version="1.0", doc=SWAGGER_ROOT, title="Core Lightning Rest", description="Core Lightning REST API Swagger", authorizations=authorizations, security=["rune"])
app.register_blueprint(blueprint)
api.add_namespace(rpcns, path="/v1")

Expand Down
1 change: 1 addition & 0 deletions plugins/clnrest/utilities/rpc_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@
plugin.add_option(name="clnrest-port", default=None, description="REST server port to listen", opt_type="int", deprecated=False)
plugin.add_option(name="clnrest-cors-origins", default="*", description="Cross origin resource sharing origins", opt_type="string", deprecated=False, multi=True)
plugin.add_option(name="clnrest-csp", default="default-src 'self'; font-src 'self'; img-src 'self' data:; frame-src 'self'; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline';", description="Content security policy (CSP) for the server", opt_type="string", deprecated=False, multi=False)
plugin.add_option(name="clnrest-swagger-root", default="/", description="Root path for Swagger UI", opt_type="string", deprecated=False, multi=False)
5 changes: 3 additions & 2 deletions plugins/clnrest/utilities/shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import pyln.client


CERTS_PATH, REST_PROTOCOL, REST_HOST, REST_PORT, REST_CSP, REST_CORS_ORIGINS = "", "", "", "", "", []
CERTS_PATH, REST_PROTOCOL, REST_HOST, REST_PORT, REST_CSP, SWAGGER_ROOT, REST_CORS_ORIGINS = "", "", "", "", "", "", []


class RuneError(Exception):
Expand Down Expand Up @@ -41,7 +41,7 @@ def validate_port(port):
def set_config(options):
if 'clnrest-port' not in options:
return "`clnrest-port` option is not configured"
global CERTS_PATH, REST_PROTOCOL, REST_HOST, REST_PORT, REST_CSP, REST_CORS_ORIGINS
global CERTS_PATH, REST_PROTOCOL, REST_HOST, REST_PORT, REST_CSP, SWAGGER_ROOT, REST_CORS_ORIGINS

REST_PORT = int(options["clnrest-port"])
if validate_port(REST_PORT) is False:
Expand All @@ -57,6 +57,7 @@ def set_config(options):

CERTS_PATH = str(options["clnrest-certs"])
REST_CSP = str(options["clnrest-csp"])
SWAGGER_ROOT = str(options["clnrest-swagger-root"])
cors_origins = options["clnrest-cors-origins"]
REST_CORS_ORIGINS.clear()
for origin in cors_origins:
Expand Down

0 comments on commit 6b98949

Please sign in to comment.