Skip to content

Commit

Permalink
Merge pull request #3 from david-engelmann/de-token
Browse files Browse the repository at this point in the history
sqld in github action
  • Loading branch information
david-engelmann authored Oct 10, 2023
2 parents 47d1e13 + 9d74c67 commit 69124c4
Show file tree
Hide file tree
Showing 12 changed files with 458 additions and 58 deletions.
31 changes: 27 additions & 4 deletions .github/workflows/test_suite.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,27 +49,50 @@ jobs:
ocaml-compiler: ${{ matrix.ocaml-compiler }}
opam-disable-sandboxing: true

- name: List all .opam files
run: find . -name "*.opam" ! -path './_opam/*'

- name: List files and perms
run: ls -laR

- name: Cache opam dependencies
uses: actions/cache@v2
id: opam-cache
with:
path: ~/.opam
key: ${{ runner.os }}-opam-${{ hashFiles('**/*.opam') }}
key: ${{ runner.os }}-opam-${{ hashFiles('*.opam') }}
restore-keys: |
${{ runner.os }}-opam-
- name: Install Common Libs
run: opam install async base uri cohttp cohttp-lwt-unix cohttp-async lwt core_kernel ssl lwt_ssl jose alcotest qcheck dune ocamlformat
run: opam install async base uri cohttp cohttp-lwt-unix cohttp-async httpaf httpaf-lwt-unix lwt core_kernel ssl sqlite3 lwt_ssl jose js_of_ocaml-compiler alcotest alcotest-lwt qcheck dune ocamlformat
# - name: Install Repo
# run: opam install . --deps-only --with-doc --with-test
- name: Dune Build
run: opam exec -- dune build
env:
AUTH_TOKEN: ${{ secrets.AUTH_TOKEN }}
LIBSQL_TOKEN: ${{ secrets.LIBSQL_TOKEN }}
LIBSQL_HOST: ${{ secrets.LIBSQL_HOST }}
LIBSQL_PORT: ${{ secrets.LIBSQL_PORT }}

- name: install rust
run: curl --proto '=https' --tlsv1.2 https://sh.rustup.rs -sSf | sh -s -- -y && . "$HOME/.cargo/env"

- name: sqld deps
run: apt-get update && apt-get install gcc g++ make libclang-dev -y

- name: sqld Build
run: git clone https://github.com/libsql/sqld.git && cd sqld && sed -i 's/setup_14/setup_18/g' ./scripts/install-deps.sh && ./scripts/install-deps.sh && . "$HOME/.cargo/env" && cargo build && ./target/debug/sqld --http-listen-addr 0.0.0.0:8000 &
env:
LIBSQL_HOST: ${{ secrets.LIBSQL_HOST }}
LIBSQL_PORT: ${{ secrets.LIBSQL_PORT }}

- name: Dune Test
run: opam exec -- dune runtest 2>&1 | tee dune_runtest.log
env:
AUTH_TOKEN: ${{ secrets.AUTH_TOKEN }}
LIBSQL_TOKEN: ${{ secrets.LIBSQL_TOKEN }}
LIBSQL_HOST: ${{ secrets.LIBSQL_HOST }}
LIBSQL_PORT: ${{ secrets.LIBSQL_PORT }}
- name: Upload Log
uses: actions/upload-artifact@v2
with:
Expand Down
2 changes: 1 addition & 1 deletion docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ RUN if [ "$USER_GID" != "1000" ] || [ "$USER_UID" != "1000" ]; then \
USER opam

RUN opam update \
&& opam install async base uri cohttp cohttp-lwt-unix cohttp-async lwt core_kernel ssl sqlite3 lwt_ssl jose alcotest qcheck dune ocamlformat
&& opam install async base uri cohttp cohttp-lwt-unix cohttp-async httpaf httpaf-lwt-unix lwt core_kernel ssl sqlite3 lwt_ssl jose js_of_ocaml-compiler alcotest alcotest-lwt qcheck dune ocamlformat
2 changes: 2 additions & 0 deletions libsql-client-ocaml.opam
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ depends: [
"lwt_ssl"
"jose"
"alcotest"
"alcotest-lwt"
"sqlite3"
"js_of_ocaml-compiler"
]
available: arch != "arm32" & arch != "x86_32"
synopsis: "OCaml-based tools for libSQL"
Expand Down
79 changes: 79 additions & 0 deletions src/auth.ml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ module Auth = struct
let token = get_token_from_env () in
{ token }

(*
let create_config : config =
let
*)

let parse_url_to_http_config (url_str : string) : config =
let url = Uri.of_string url_str in
let scheme = Uri.scheme url |> Option.value ~default:"" in
Expand Down Expand Up @@ -139,4 +144,78 @@ module Auth = struct
(fun body -> Lwt.return (Ok body))

let close_client client = { client with url = Uri.of_string "" }

let create_auth_endpoint (query_name : string) (version : string) : string =
"/" ^ version ^ "/" ^ "auth" ^ "/" ^ query_name

let get_all_api_tokens (c : client) =
let api_tokens_endpoint = create_auth_endpoint "api_tokens" "v1" in
if Uri.to_string c.url = "" then
raise (Http_clientError ("The client URL is empty", "CLIENT_CLOSED"));

let uri_with_query = Uri.with_path c.url api_tokens_endpoint in
let headers =
Cohttp_client.create_headers_from_pairs
[ ("Content-Type", "application/json") ]
in
let headers =
match c.config.auth_token with
| Some token ->
Cohttp.Header.add_list headers
[ create_bearer_auth_header (create_auth_from_token token) ]
| None -> headers
in

Lwt.bind
(Cohttp_client.get_request_with_headers
(Uri.to_string uri_with_query)
headers)
(fun body -> Lwt.return (Ok body))

let create_auth_token (c : client) (name : string) =
let api_tokens_endpoint = create_auth_endpoint "api_tokens" "v1" in
if Uri.to_string c.url = "" then
raise (Http_clientError ("The client URL is empty", "CLIENT_CLOSED"));
let uri_with_query = Uri.with_path c.url api_tokens_endpoint in
let uri_with_query = Uri.with_path uri_with_query name in

let headers =
Cohttp_client.create_headers_from_pairs
[ ("Content-Type", "application/json") ]
in
let headers =
match c.config.auth_token with
| Some token ->
Cohttp.Header.add_list headers
[ create_bearer_auth_header (create_auth_from_token token) ]
| None -> headers
in

Lwt.bind
(Cohttp_client.post_request_with_headers
(Uri.to_string uri_with_query)
headers)
(fun body -> Lwt.return (Ok body))

let get_health (c : client) =
if Uri.to_string c.url = "" then
raise (Http_clientError ("The client URL is empty", "CLIENT_CLOSED"));
let uri_with_query = Uri.with_path c.url "health" in
let headers =
Cohttp_client.create_headers_from_pairs
[ ("Content-Type", "application/json") ]
in
let headers =
match c.config.auth_token with
| Some token ->
Cohttp.Header.add_list headers
[ create_bearer_auth_header (create_auth_from_token token) ]
| None -> headers
in

Lwt.bind
(Cohttp_client.get_request_with_headers
(Uri.to_string uri_with_query)
headers)
(fun body -> Lwt.return (Ok body))
end
6 changes: 4 additions & 2 deletions src/dune
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
(include_subdirs unqualified)
(library
(name libsql)
(libraries async core uri cohttp cohttp-lwt-unix cohttp-async httpaf httpaf-lwt-unix ssl sqlite3 jose lwt_ssl lwt)
(libraries async core uri cohttp cohttp-lwt-unix cohttp-async httpaf httpaf-lwt-unix ssl sqlite3 jose js_of_ocaml-compiler lwt_ssl lwt)
(preprocess (pps ppx_jane))
(modules Auth Cohttp_client Hrana Path Url Sqlite))


;;(executable (name libsql))
;; (modes byte exe))
61 changes: 60 additions & 1 deletion src/hrana.ml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ module Hrana = struct

let parse_url_to_hrana_config (url_str : string) : config =
let url = Uri.of_string url_str in
let scheme = Uri.scheme url |> Option.value ~default:"" in
let scheme_value = Uri.scheme url |> Option.value ~default:"" in
let authority = Uri.host_with_default ~default:"" url in
let path = Uri.path url in
let initial_auth_token = Auth.get_token_from_env () in
Expand All @@ -40,6 +40,12 @@ module Hrana = struct
| "0" | "false" -> false
| _ -> false
in
let scheme =
match String.lowercase_ascii scheme_value with
| "libsql" when tls -> "wss"
| "libsql" -> "ws"
| _ -> scheme_value
in
{ scheme; authority; path; auth_token; tls }

let validate_hrana_config (config : config) : unit =
Expand Down Expand Up @@ -97,4 +103,57 @@ module Hrana = struct
(fun body -> Lwt.return (Ok body))

let close_client client = { client with url = Uri.of_string "" }

let get_all_api_tokens (c : client) =
let api_tokens_endpoint = Auth.create_auth_endpoint "api_tokens" "v1" in
if Uri.to_string c.url = "" then
raise (HranaError ("The client URL is empty", "CLIENT_CLOSED"));

let uri_with_query = Uri.with_path c.url api_tokens_endpoint in
let headers =
Cohttp_client.create_headers_from_pairs
[ ("Content-Type", "application/json") ]
in
let headers =
match c.config.auth_token with
| Some token ->
Cohttp.Header.add_list headers
[
Auth.create_bearer_auth_header (Auth.create_auth_from_token token);
]
| None -> headers
in

Lwt.bind
(Cohttp_client.get_request_with_headers
(Uri.to_string uri_with_query)
headers)
(fun body -> Lwt.return (Ok body))

let create_auth_token (c : client) (name : string) =
let api_tokens_endpoint = Auth.create_auth_endpoint "api_tokens" "v1" in
if Uri.to_string c.url = "" then
raise (HranaError ("The client URL is empty", "CLIENT_CLOSED"));
let uri_with_query = Uri.with_path c.url api_tokens_endpoint in
let uri_with_query = Uri.with_path uri_with_query name in

let headers =
Cohttp_client.create_headers_from_pairs
[ ("Content-Type", "application/json") ]
in
let headers =
match c.config.auth_token with
| Some token ->
Cohttp.Header.add_list headers
[
Auth.create_bearer_auth_header (Auth.create_auth_from_token token);
]
| None -> headers
in

Lwt.bind
(Cohttp_client.post_request_with_headers
(Uri.to_string uri_with_query)
headers)
(fun body -> Lwt.return (Ok body))
end
14 changes: 10 additions & 4 deletions test/dune
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
(tests
; multiple test files
(names test_auth test_hrana test_path test_sqlite test_url)
(names test_auth test_hrana
;;test_https_hrana test_http_auth
test_https_auth test_path test_sqlite test_url)

(libraries async core uri cohttp cohttp-lwt-unix cohttp-async httpaf httpaf-lwt-unix ssl sqlite3 jose lwt_ssl lwt alcotest libsql)
(libraries async core uri cohttp cohttp-lwt-unix cohttp-async httpaf httpaf-lwt-unix ssl sqlite3 jose js_of_ocaml-compiler lwt_ssl lwt alcotest alcotest-lwt libsql)

; multiple test files
(modules test_auth test_hrana test_path test_sqlite test_url))

(modules test_auth test_hrana
;;test_https_hrana test_http_auth
test_https_auth test_path test_sqlite test_url))

;(executable (name test_libsql)
; (modes byte exe)))
86 changes: 42 additions & 44 deletions test/test_auth.ml
Original file line number Diff line number Diff line change
Expand Up @@ -90,48 +90,46 @@ let test_validate_http_config_https =
test_case "validate_http_config with https does not raise an exception" `Quick
(fun () -> Auth.validate_http_config sample_http_config_https)

let sync_suite =
[
("test_sample_basic_cred_defaults", `Quick, test_sample_basic_cred_defaults);
("test_sample_basic_cred", `Quick, test_sample_basic_cred);
("test_get_base_url_from_env", `Quick, test_get_base_url_from_env);
("test_get_token_from_env", `Quick, test_get_token_from_env);
( "test_sample_http_config_http_scheme",
`Quick,
test_sample_http_config_http_scheme );
( "test_sample_http_config_http_authority",
`Quick,
test_sample_http_config_http_authority );
( "test_sample_http_config_http_path",
`Quick,
test_sample_http_config_http_path );
( "test_sample_http_config_http_auth_token",
`Quick,
test_sample_http_config_http_auth_token );
( "test_sample_http_config_http_tls",
`Quick,
test_sample_http_config_http_tls );
( "test_sample_http_config_https_scheme",
`Quick,
test_sample_http_config_https_scheme );
( "test_sample_http_config_https_authority",
`Quick,
test_sample_http_config_https_authority );
( "test_sample_http_config_https_path",
`Quick,
test_sample_http_config_https_path );
( "test_sample_http_config_https_auth_token",
`Quick,
test_sample_http_config_https_auth_token );
( "test_sample_http_config_https_tls",
`Quick,
test_sample_http_config_https_tls );
test_validate_http_config_http;
test_validate_http_config_https;
]

let () =
Alcotest.run "Auth Test Suite"
[
( "Auth",
[
( "test_sample_basic_cred_defaults",
`Quick,
test_sample_basic_cred_defaults );
("test_sample_basic_cred", `Quick, test_sample_basic_cred);
("test_get_base_url_from_env", `Quick, test_get_base_url_from_env);
("test_get_token_from_env", `Quick, test_get_token_from_env);
( "test_sample_http_config_http_scheme",
`Quick,
test_sample_http_config_http_scheme );
( "test_sample_http_config_http_authority",
`Quick,
test_sample_http_config_http_authority );
( "test_sample_http_config_http_path",
`Quick,
test_sample_http_config_http_path );
( "test_sample_http_config_http_auth_token",
`Quick,
test_sample_http_config_http_auth_token );
( "test_sample_http_config_http_tls",
`Quick,
test_sample_http_config_http_tls );
( "test_sample_http_config_https_scheme",
`Quick,
test_sample_http_config_https_scheme );
( "test_sample_http_config_https_authority",
`Quick,
test_sample_http_config_https_authority );
( "test_sample_http_config_https_path",
`Quick,
test_sample_http_config_https_path );
( "test_sample_http_config_https_auth_token",
`Quick,
test_sample_http_config_https_auth_token );
( "test_sample_http_config_https_tls",
`Quick,
test_sample_http_config_https_tls );
test_validate_http_config_http;
test_validate_http_config_https;
] );
]
print_endline "Running Auth Sync Suite";
Alcotest.run "Auth Sync Test Suite" [ ("AuthSync", sync_suite) ]
Loading

0 comments on commit 69124c4

Please sign in to comment.