Skip to content

Commit 32ef686

Browse files
bkioshnstevenj
andauthored
fix: API cleanup (#1036)
* fix: api endpoint draft Signed-off-by: bkioshn <[email protected]> * fix: api health endpoint v1 Signed-off-by: bkioshn <[email protected]> * fix: remove bad request from errorResponses Signed-off-by: bkioshn <[email protected]> * fix: add bad req to get /registration Signed-off-by: bkioshn <[email protected]> * fix: error logging Signed-off-by: bkioshn <[email protected]> * fix: remove validation error Signed-off-by: bkioshn <[email protected]> * fix: registration get error name Signed-off-by: bkioshn <[email protected]> * chore:format Signed-off-by: bkioshn <[email protected]> * fix: get json schema from openapi spec Signed-off-by: bkioshn <[email protected]> * fix: move schema utils Signed-off-by: bkioshn <[email protected]> * fix: optional field Signed-off-by: bkioshn <[email protected]> * fix: config key Signed-off-by: bkioshn <[email protected]> * fix: cat-gateway code gen Signed-off-by: bkioshn <[email protected]> * fix: api name in cat-voice Signed-off-by: bkioshn <[email protected]> * fix: cat-voice format Signed-off-by: bkioshn <[email protected]> * chore: fix spacing Signed-off-by: bkioshn <[email protected]> * chore: fix spacing Signed-off-by: bkioshn <[email protected]> * chore: change tag config description * test: add test for default validator * fix: add spectral ruleset Signed-off-by: bkioshn <[email protected]> * fix(cat-gateway): Sort the spelling words, and use latest deny.toml * fix(cat-gateway): Fix broken pre-push justfile target * docs(cat-gateway): cleanup * docs(cat-gateway): Fix API Groups and document them better * docs(cat-gateway): Add documentation to the health/inspection endpoint * docs(cat-gateway): Add descriptions for cardano/cip36/latest_registration/stake_addr * docs(cat-gateway): Document stake key hash and vote key endpoints for cardano * docs(cat-gateway): add documentation to config/frontend * docs(cat-gateway): Add api docs for frontend schema * docs(cat-gateway): Move legacy registration endpoints into the Legacy TAG. * docs(cat-gateway): Remaining documentable entities documented * fix: update openapi linter Signed-off-by: bkioshn <[email protected]> * docs(cat-gateway): Add more constraints to parameters and json bodies * fix: openapi lint FUNCTION name Signed-off-by: bkioshn <[email protected]> * fix: CIP36 example and description Signed-off-by: bkioshn <[email protected]> * fix(cat-gateway): cleanup error handling, and add a global 429 response to all endpoints. * fix: config endpoint example, desc, and return Signed-off-by: bkioshn <[email protected]> * chore: remove todo Signed-off-by: bkioshn <[email protected]> * fix: move config object Signed-off-by: bkioshn <[email protected]> * fix: move cip36 object Signed-off-by: bkioshn <[email protected]> * docs(cat-gateway): Add missing headers to responses * docs(cat-gateway): Cleanup the rest of the documentation in the api * fix(cat-gateway): Fix OpenAPI linting and add autogenerated api file for dart. * refactor(cat-gateway): Better generalize the OpenAPI simple string type creation macro. * fix(cat-gateway): Add APIKey and CatToken auth to some endpoints. Add 401 and 403 common responses. * fix(cat-gateway): Add universal 422 response to all endpoints, and try and make all endpoint validation use it. * fix: add cardano stake address type Signed-off-by: bkioshn <[email protected]> * fix(cat-gateway): stake address type Signed-off-by: bkioshn <[email protected]> * fix(cat-gateway): Refactor the RBAC Token auth, so it's easier to maintain. * fix(cat-gateway): stake address name Signed-off-by: bkioshn <[email protected]> * fix(cat-gateway): Add no auth and no-auth+rbac auth schemes * fix(cat-gateway): format + stake addr example Signed-off-by: bkioshn <[email protected]> * fix(cat-gateway): code format * fix(cat-gateway): openapi spectral example rules Signed-off-by: bkioshn <[email protected]> --------- Signed-off-by: bkioshn <[email protected]> Co-authored-by: Steven Johnson <[email protected]> Co-authored-by: Steven Johnson <[email protected]>
1 parent e257af5 commit 32ef686

File tree

77 files changed

+3366
-1100
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

77 files changed

+3366
-1100
lines changed

.config/dictionaries/project.dic

+2
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ oneshot
181181
openapi
182182
opentelemetry
183183
overprovisioned
184+
Pbkdf2
184185
pbxproj
185186
Pdart
186187
permissionless
@@ -205,6 +206,7 @@ pubspec
205206
pytest
206207
qrcode
207208
rapidoc
209+
ratelimit
208210
redoc
209211
reloadable
210212
Replayability

catalyst-gateway/Earthfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ build:
8181
all-hosts-build:
8282
BUILD --platform=linux/amd64 --platform=linux/arm64 +build
8383

84-
# package-cat-gateway : Create a deployable container for catalyst-gateway
84+
# package : Create a deployable container for catalyst-gateway
8585
package:
8686
FROM debian:12.7-slim
8787
WORKDIR /cat-gateway

catalyst-gateway/Justfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ pre-push: sync-cfg code-format code-lint license-check
3434
# Make sure we can actually build inside Earthly which needs to happen in CI.
3535
cd .. && earthly ./catalyst-gateway+check
3636
cd .. && earthly ./catalyst-gateway+build
37-
cd .. && earthly ./catalyst-gateway+package-cat-gateway
37+
cd .. && earthly ./catalyst-gateway+package
3838
cd .. && earthly ./catalyst-gateway/tests+test-lint-openapi
3939

4040
# Build Local release build of catalyst gateway

catalyst-gateway/README.md

+2-3
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,12 @@ or you can build a docker image and run everything with the `docker-compose`.
4040

4141
To build and run docker images follow these steps:
4242

43-
1. Run `earthly +package-cat-gateway` or `earthly +package-cat-gateway-with-preprod-snapshot`
44-
to build a cat-gateway docker image without `preprod-snapshot` or with it.
43+
1. Run `earthly +package` to build a cat-gateway docker image.
4544
2. Run `earthly ./event-db+build` to build an event-db docker image.
4645
3. Run `docker-compose up cat-gateway` to spin up cat-gateway with event-db from already built images.
4746

4847
Note that every time when you are building an image it obsoletes an old image but does not remove it,
49-
so dont forget to cleanup dangling images of the event-db and cat-gateway in your docker environment.
48+
so don't forget to clean up dangling images of the event-db and cat-gateway in your docker environment.
5049

5150
### Rust binary
5251

catalyst-gateway/bin/src/db/event/config/jsonschema/frontend.json

-30
This file was deleted.

catalyst-gateway/bin/src/db/event/config/key.rs

+68-22
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ use jsonschema::{BasicOutput, Validator};
66
use serde_json::{json, Value};
77
use tracing::error;
88

9+
use crate::utils::schema::{extract_json_schema_for, SCHEMA_VERSION};
10+
911
/// Configuration key
1012
#[derive(Debug, Clone, PartialEq)]
1113
pub(crate) enum ConfigKey {
@@ -24,9 +26,9 @@ impl Display for ConfigKey {
2426
}
2527
}
2628

27-
/// Frontend schema.
28-
static FRONTEND_SCHEMA: LazyLock<Value> =
29-
LazyLock::new(|| load_json_lazy(include_str!("jsonschema/frontend.json")));
29+
/// Frontend schema from API specification.
30+
pub(crate) static FRONTEND_SCHEMA: LazyLock<Value> =
31+
LazyLock::new(|| extract_json_schema_for("FrontendConfig"));
3032

3133
/// Frontend schema validator.
3234
static FRONTEND_SCHEMA_VALIDATOR: LazyLock<Validator> =
@@ -43,28 +45,35 @@ static FRONTEND_IP_DEFAULT: LazyLock<Value> =
4345
/// Helper function to create a JSON validator from a JSON schema.
4446
/// If the schema is invalid, a default JSON validator is created.
4547
fn schema_validator(schema: &Value) -> Validator {
46-
jsonschema::validator_for(schema).unwrap_or_else(|err| {
47-
error!(
48-
id = "schema_validator",
49-
error=?err,
50-
"Error creating JSON validator"
51-
);
52-
53-
// Create a default JSON validator as a fallback
54-
// This should not fail since it is hard coded
55-
#[allow(clippy::expect_used)]
56-
Validator::new(&json!({
57-
"$schema": "http://json-schema.org/draft-07/schema#",
58-
"type": "object"
59-
}))
60-
.expect("Failed to create default JSON validator")
61-
})
48+
Validator::options()
49+
.with_draft(jsonschema::Draft::Draft202012)
50+
.build(schema)
51+
.unwrap_or_else(|err| {
52+
error!(
53+
id="schema_validator",
54+
error=?err,
55+
"Error creating JSON validator"
56+
);
57+
58+
default_validator()
59+
})
60+
}
61+
62+
/// Create a default JSON validator as a fallback
63+
/// This should not fail since it is hard coded
64+
fn default_validator() -> Validator {
65+
#[allow(clippy::expect_used)]
66+
Validator::new(&json!({
67+
"$schema": SCHEMA_VERSION,
68+
"type": "object"
69+
}))
70+
.expect("Failed to create default JSON validator")
6271
}
6372

6473
/// Helper function to convert a JSON string to a JSON value.
6574
fn load_json_lazy(data: &str) -> Value {
6675
serde_json::from_str(data).unwrap_or_else(|err| {
67-
error!(id = "load_json_lazy", error=?err, "Error parsing JSON");
76+
error!(id="load_json_lazy", error=?err, "Error parsing JSON");
6877
json!({})
6978
})
7079
}
@@ -115,15 +124,43 @@ mod tests {
115124
use super::*;
116125

117126
#[test]
118-
fn test_valid_validate() {
127+
fn test_schema_for_schema() {
128+
// Invalid schema
129+
let invalid_schema = json!({
130+
"title": "Invalid Example Schema",
131+
"type": "object",
132+
133+
"properties": {
134+
"invalidProperty": {
135+
"type": "unknownType"
136+
}
137+
},
138+
139+
});
140+
// This should not fail
141+
schema_validator(&invalid_schema);
142+
}
143+
144+
#[test]
145+
fn test_valid_validate_1() {
119146
let value = json!({
120-
"test": "test"
147+
"sentry": {
148+
"dsn": "https://test.com"
149+
}
121150
});
122151
let result = ConfigKey::Frontend.validate(&value);
123152
assert!(result.is_valid());
124153
println!("{:?}", serde_json::to_value(result).unwrap());
125154
}
126155

156+
#[test]
157+
fn test_valid_validate_2() {
158+
let value = json!({});
159+
let result = ConfigKey::Frontend.validate(&value);
160+
assert!(result.is_valid());
161+
println!("{:?}", serde_json::to_value(result).unwrap());
162+
}
163+
127164
#[test]
128165
fn test_invalid_validate() {
129166
let value = json!([]);
@@ -137,4 +174,13 @@ mod tests {
137174
let result = ConfigKey::Frontend.default();
138175
assert!(result.is_object());
139176
}
177+
178+
#[test]
179+
fn test_default_validator() {
180+
let result = std::panic::catch_unwind(|| {
181+
default_validator();
182+
});
183+
// Assert that no panic occurred
184+
assert!(result.is_ok(), "default_validator panicked");
185+
}
140186
}

catalyst-gateway/bin/src/service/api/auth/endpoint.rs

-128
This file was deleted.

catalyst-gateway/bin/src/service/api/auth/mod.rs

-4
This file was deleted.

0 commit comments

Comments
 (0)