Skip to content

Commit a5cd110

Browse files
committed
remove socketio
1 parent ae7af26 commit a5cd110

File tree

29 files changed

+394
-580
lines changed

29 files changed

+394
-580
lines changed

cli/Cargo.lock

Lines changed: 23 additions & 277 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cli/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ serde_json = "1.0.107"
2626
lazy_static = "1.4.0"
2727
serde = { version = "1.0.189", features = ["derive"] }
2828
toml = "0.8.2"
29-
rust_socketio = { version = "0.4.3", features = ["async"] }
3029
futures-util = "0.3.28"
3130
http-body-util = "0.1.0-rc.3"
3231
bytes = "1.5.0"
@@ -35,6 +34,9 @@ dirs = "5.0.1"
3534
chrono = "0.4.31"
3635
once_cell = "1.18.0"
3736
hyper-tls = "0.5.0"
37+
base64 = "0.21.5"
38+
tokio-tungstenite = { version = "*", features = ["native-tls"] }
39+
futures-channel = "0.3.29"
3840

3941
[dev-dependencies]
4042
serial_test = "2.0.0"

cli/README.md

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ This project can be installed and compiled from source with this Cargo command:
2020
```
2121
$ cargo install wasmo
2222
or
23-
$ brew install wasmo (coming soon)
23+
$ brew tap maif/wasmo
24+
$ brew install wasmo
2425
```
2526

2627
Additionally there are [precompiled artifacts built on CI][artifacts] which are
@@ -47,17 +48,17 @@ with wasm modules:
4748

4849
| Tool | Arguments | Description |
4950
| ---------------------------- | -- | ------------------------------------------------------------- |
50-
| `wasmo config set` | token | path | server | Globally configure the CLI with the authorization token, the path where the configuration file will be stored and the server to reach during the build. These parameters are optional and can be passed when running the build command. |
51+
| `wasmo config set` | path | server | clientId | clientSecret | Globally configure the CLI with the path where the configuration file will be stored and the server to reach during the build. These parameters are optional and can be passed when running the build command. |
5152
| `wasmo config get` | | Get the configuration from the configured path file or from `$HOME/.wasmo` |
5253
| `wasmo config reset` | | Clean configuration and reset to default settings. The default file path configuration will be `$HOME/.wasmo` ||
5354
| `wasmo init` | template | name | path | Initialize a WASM plugin to the specific path. You can choose between many templates, javascript/typescript (js/ts), Open Policy Agent (opa), Rust or Golang (go). |
54-
| `wasmo build` | path | host | server | token | Build the plugin by sending the contents to the remote or local Wasmo server. As soon as the build is complete, the WASM binary is donwloaded and saved in the plugin folder. |
55+
| `wasmo build` | path | host | server | clientId | clientSecret | Build the plugin by sending the contents to the remote or local Wasmo server. As soon as the build is complete, the WASM binary is donwloaded and saved in the plugin folder. |
5556

5657
# Quick start
5758

5859
```
5960
wasmo init --name=my-first-plugin --templates=js
60-
wasmo build --host=OneShotDocker --path=my-first-plugin --token=foobar
61+
wasmo build --host=OneShotDocker --path=my-first-plugin
6162
```
6263

6364
Then open the content of your `my-first-plugin` folder. You should find the generated WASM binary named `my-first-plugin-1.0.0.wasm`.
@@ -84,12 +85,11 @@ You have two ways to build your plugin:
8485

8586
[wasmoserver]: https://github.com/MAIF/wasmo
8687

87-
Assuming we want to build our `my-first-plugin` locally. Enter `wasmo build --host=OneShotDocker --path=my-first-plugin --token=foobar` to start the build.
88+
Assuming we want to build our `my-first-plugin` locally. Enter `wasmo build --host=OneShotDocker --path=my-first-plugin` to start the build.
8889

8990
Let's explain these 3 parameters:
9091
- the `path` parameter is explicitly used to indicate the plugin to build
9192
- the `host` indicates which kind of Wasmo server used. The pratical way is to use `Docker` or `OneShotDocker` because it prevents to install a Wasmo server by deploying, inside your locally Docker, a new Wasmo container. The last possible value is `Remote` and can be used to specify with a URI the remote Wasmo server used.
92-
- the `token` argument is passed to the Wasmo server to authenticate the CLI. It can also be set up in the [configuration file][#configure-your-configuration-file]
9393

9494
[![IMAGE ALT TEXT HERE](https://img.youtube.com/vi/NdbQR6vQ5Sk/0.jpg)](https://www.youtube.com/watch?v=NdbQR6vQ5Sk)
9595

@@ -123,14 +123,12 @@ If value is omitted, then it sets it to an empty string.
123123
The available keys are:
124124
- `path`: configure the path where the wasmo configuration will be stored
125125
- `server`: the Wasmo server to build your plugins (an URL format value is expected)
126-
- `token`: the token, pass to the Wasmo server, to authenticate the CLI
127-
- `docker_authorization`: the token value accepted by Docker instances
126+
- `clientId`: the client id used in Basic and Otoroshi Auth
127+
- `clientSecret`: the client secret used in Basic and Otoroshi Auth
128128

129129
You can also edit the configuration file manually. In this case, the following values are :
130130
- `path` = WASMO_PATH
131131
- `server` = WASMO_SERVER
132-
- `token` = WASMO_TOKEN
133-
- `docker_authorization` = DOCKER_AUTHORIZATION
134132

135133
#### get
136134

cli/src/bin.rs

Lines changed: 68 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ mod error;
55
mod port;
66
mod logger;
77

8+
use base64::{engine::general_purpose, Engine as _};
89
use clap::{Parser, Subcommand};
910
use error::{WasmoError, WasmoResult};
1011
use hyper_tls::HttpsConnector;
@@ -22,8 +23,8 @@ use dirs;
2223

2324
const WASMO_SERVER: &str = "WASMO_SERVER";
2425
const WASMO_PATH: &str = "WASMO_PATH";
25-
const WASMO_TOKEN: &str = "WASMO_TOKEN";
26-
const WASMO_AUTHORIZATION_HEADER: &str = "WASMO_AUTHORIZATION_HEADER";
26+
const WASMO_CLIENT_ID: &str = "WASMO_CLIENT_ID";
27+
const WASMO_CLIENT_SECRET: &str = "WASMO_CLIENT_SECRET";
2728

2829
const ZIP_GO: &[u8] = include_bytes!("../templates/go.zip");
2930
const ZIP_JS: &[u8] = include_bytes!("../templates/js.zip");
@@ -140,15 +141,22 @@ enum Commands {
140141
required = false
141142
)]
142143
host: String,
143-
/// token
144+
/// client id
144145
#[arg(
145-
value_name = "TOKEN",
146-
long = "token",
146+
value_name = "CLIENT_ID",
147+
long = "clientId",
147148
required = false
148149
)]
149-
token: Option<String>,
150+
client_id: Option<String>,
151+
/// client secret
152+
#[arg(
153+
value_name = "CLIENT_SECRET",
154+
long = "clientSecret",
155+
required = false
156+
)]
157+
client_secret: Option<String>,
150158
},
151-
/// Globally configure the CLI with the authorization token, the path where the configuration file will be stored and the server to reach during the build. These parameters are optional and can be passed when running the build command.
159+
/// Globally configure the CLI with the path where the configuration file will be stored and the server to reach during the build. These parameters are optional and can be passed when running the build command.
152160
Config {
153161
#[command(subcommand)]
154162
command: ConfigCommands,
@@ -169,15 +177,6 @@ enum ConfigCommands {
169177
170178
)]
171179
path: Option<String>,
172-
/// The token access, used to authenticate the calls to the server
173-
#[arg(
174-
value_name = "TOKEN",
175-
short = 't',
176-
long = "token",
177-
help = "The token access, used to authenticate the calls to the server",
178-
required = false
179-
)]
180-
token: Option<String>,
181180
/// The remote server to build your plugins
182181
#[arg(
183182
value_name = "SERVER",
@@ -187,16 +186,20 @@ enum ConfigCommands {
187186
required = false
188187
)]
189188
server: Option<String>,
190-
/// The cli authorization send to the remote or local builder
189+
/// client id
191190
#[arg(
192-
value_name = "DOCKER_AUTHORIZATION",
193-
short = 'd',
194-
long = "docker_authorization",
195-
help = "The token expected by the docker wasmo container when building. The default value is foobar",
191+
value_name = "CLIENT_ID",
192+
long = "clientId",
196193
required = false
197194
)]
198-
docker_authorization: Option<String>,
199-
195+
client_id: Option<String>,
196+
/// client secret
197+
#[arg(
198+
value_name = "CLIENT_SECRET",
199+
long = "clientSecret",
200+
required = false
201+
)]
202+
client_secret: Option<String>,
200203
},
201204
Get {},
202205
}
@@ -339,10 +342,11 @@ fn configuration_file_to_hashmap(configuration_path: &PathBuf) -> HashMap<String
339342
}
340343

341344
fn read_configuration() -> WasmoResult<HashMap<String, String>> {
342-
let wasmo_token = option_env!("WASMO_TOKEN");
345+
let wasmo_client_id = option_env!("WASMO_CLIENT_ID");
346+
let wasmo_client_secret = option_env!("WASMO_CLIENT_SECRET");
343347
let wasmo_server = option_env!("WASMO_SERVER");
344348

345-
let envs: HashMap<String, String> = if wasmo_server.is_none() || wasmo_server.is_none() {
349+
let envs: HashMap<String, String> = if wasmo_server.is_none() || wasmo_client_id.is_none() || wasmo_client_secret.is_none() {
346350
let configuration_path: PathBuf = match option_env!("WASMO_PATH") {
347351
Some(path) => Path::new(path).to_path_buf(),
348352
None => get_home().unwrap(),
@@ -357,15 +361,16 @@ fn read_configuration() -> WasmoResult<HashMap<String, String>> {
357361
} else {
358362
let mut envs: HashMap<String, String> = HashMap::new();
359363
envs.insert(WASMO_SERVER.to_owned(), wasmo_server.unwrap().to_owned());
360-
envs.insert(WASMO_TOKEN.to_owned(), wasmo_token.unwrap().to_owned());
364+
envs.insert(WASMO_CLIENT_ID.to_owned(), wasmo_client_id.unwrap().to_owned());
365+
envs.insert(WASMO_CLIENT_SECRET.to_owned(), wasmo_client_secret.unwrap().to_owned());
361366
envs.insert(WASMO_PATH.to_owned(), get_option_home());
362367
envs
363368
};
364369

365370
Ok(envs)
366371
}
367372

368-
async fn build(path: Option<String>, server: Option<String>, host: Host, token: Option<String>) -> WasmoResult<()> {
373+
async fn build(path: Option<String>, server: Option<String>, host: Host, client_id: Option<String>, client_secret: Option<String>) -> WasmoResult<()> {
369374
let mut configuration = read_configuration()?;
370375

371376
let complete_path = match path {
@@ -377,12 +382,12 @@ async fn build(path: Option<String>, server: Option<String>, host: Host, token:
377382
configuration.insert(WASMO_SERVER.to_owned(), server.unwrap().to_owned());
378383
}
379384

380-
if token.is_some() {
381-
configuration.insert(WASMO_TOKEN.to_owned(), token.unwrap().to_owned());
385+
if client_id.is_some() {
386+
configuration.insert(WASMO_CLIENT_ID.to_owned(), client_id.unwrap().to_owned());
382387
}
383388

384-
if !configuration.contains_key(WASMO_TOKEN) {
385-
return Err(WasmoError::BuildInterrupt("Should be able to build until WASMO_TOKEN is not defined".to_string()));
389+
if client_secret.is_some() {
390+
configuration.insert(WASMO_CLIENT_SECRET.to_owned(), client_secret.unwrap().to_owned());
386391
}
387392

388393
if !Path::new(&complete_path).exists() {
@@ -392,7 +397,7 @@ async fn build(path: Option<String>, server: Option<String>, host: Host, token:
392397
let mut container = None;
393398

394399
if host != Host::Remote {
395-
container = Some(docker::docker_create(&host, &configuration).await?);
400+
container = Some(docker::docker_create(&host).await?);
396401

397402
configuration.insert(WASMO_SERVER.to_owned(), format!("http://localhost:{}", &container.as_ref().unwrap().port).to_string());
398403
}
@@ -414,12 +419,10 @@ async fn build(path: Option<String>, server: Option<String>, host: Host, token:
414419
configuration.get(WASMO_SERVER).unwrap()
415420
))
416421
.header("Content-Type", "application/json")
417-
.header(
418-
configuration
419-
.get(WASMO_AUTHORIZATION_HEADER)
420-
.unwrap_or(&"Authorization".to_string()),
421-
configuration.get(WASMO_TOKEN).unwrap(),
422-
)
422+
.header("Authorization",
423+
general_purpose::STANDARD_NO_PAD.encode(format!("{}:{}",
424+
configuration.get(WASMO_CLIENT_ID).unwrap_or(&"".to_string()),
425+
configuration.get(WASMO_CLIENT_SECRET).unwrap_or(&"".to_string()))))
423426
.body(Body::from(serde_json::to_string(&plugin).unwrap()))
424427
.unwrap();
425428

@@ -434,8 +437,8 @@ async fn build(path: Option<String>, server: Option<String>, host: Host, token:
434437
Ok(k) => {
435438
logger::log(format!("Build call status: {}", k.status()));
436439

437-
if k.status() == 403 {
438-
return Err(WasmoError::BuildInterrupt("".to_string()));
440+
if k.status() == 403 || k.status() == 401 {
441+
return Err(WasmoError::BuildInterrupt("unauthorized".to_string()));
439442
}
440443

441444
let body_bytes = hyper::body::to_bytes(k.into_body()).await;
@@ -483,12 +486,11 @@ async fn get_wasm(
483486

484487
let request = Request::get(url)
485488
.header("Content-Type", "application/json")
486-
.header(
487-
configuration
488-
.get(WASMO_AUTHORIZATION_HEADER)
489-
.unwrap_or(&"Authorization".to_string()),
490-
configuration.get(WASMO_TOKEN).unwrap(),
491-
)
489+
.header("Authorization",
490+
general_purpose::STANDARD_NO_PAD.encode(format!("{}:{}",
491+
configuration.get(WASMO_CLIENT_ID).unwrap_or(&"".to_string()),
492+
configuration.get(WASMO_CLIENT_SECRET).unwrap_or(&"".to_string()))
493+
))
492494
.body(Body::empty())
493495
.unwrap();
494496

@@ -581,10 +583,10 @@ fn expand_tilde<P: AsRef<Path>>(path_user_input: P) -> Option<PathBuf> {
581583
})
582584
}
583585

584-
fn set_configuration(token: Option<String>, server: Option<String>, path: Option<String>, docker_authorization: Option<String>) -> WasmoResult<()> {
586+
fn set_configuration(server: Option<String>, path: Option<String>, client_id: Option<String>, client_secret: Option<String>) -> WasmoResult<()> {
585587

586-
if token.is_none() && server.is_none() && path.is_none() && docker_authorization.is_none() {
587-
return Err(WasmoError::Configuration("missing token, server, path or docker_authorization keys".to_string()));
588+
if client_id.is_none() && client_secret.is_none() && server.is_none() && path.is_none() {
589+
return Err(WasmoError::Configuration("missing client_id, client_secret or path keys".to_string()));
588590
}
589591

590592
let home_path = get_home()?;
@@ -604,16 +606,16 @@ fn set_configuration(token: Option<String>, server: Option<String>, path: Option
604606
true => configuration_file_to_hashmap(&complete_path),
605607
};
606608

607-
let wasmo_token = extract_variable_or_default(&contents, WASMO_TOKEN, token);
609+
let wasmo_client_id = extract_variable_or_default(&contents, WASMO_CLIENT_ID, client_id);
610+
let wasmo_client_secret = extract_variable_or_default(&contents, WASMO_CLIENT_SECRET, client_secret);
608611
let wasmo_server = extract_variable_or_default(&contents, WASMO_SERVER, server);
609612
let wasmo_path = extract_variable_or_default(&contents, "WASMO_PATH", path.clone());
610-
let wasmo_docker_authorization = extract_variable_or_default(&contents, "DOCKER_AUTHORIZATION", path.clone());
611613

612614
if wasmo_path.eq("") {
613-
let new_content = format!("WASMO_TOKEN={}\nWASMO_SERVER={}\nDOCKER_AUTHORIZATION={}",
614-
wasmo_token,
615-
wasmo_server,
616-
wasmo_docker_authorization);
615+
let new_content = format!("WASMO_SERVER={}\nWASMO_CLIENT_ID={}\nWASMO_CLIENT_SECRET={}",
616+
wasmo_server,
617+
wasmo_client_id,
618+
wasmo_client_secret);
617619

618620
match fs::write(home_path.join(".wasmo"), new_content) {
619621
Ok(()) => logger::println(format!("wasmo configuration patched")),
@@ -624,8 +626,8 @@ fn set_configuration(token: Option<String>, server: Option<String>, path: Option
624626
}
625627
} else {
626628
let content_at_path = format!(
627-
"WASMO_TOKEN={}\nWASMO_SERVER={}\nWASMO_PATH={}\nDOCKER_AUTHORIZATION={}",
628-
wasmo_token, wasmo_server, wasmo_path, wasmo_docker_authorization
629+
"WASMO_SERVER={}\nWASMO_PATH={}\nWASMO_CLIENT_ID={}\nWASMO_CLIENT_SECRET={}",
630+
wasmo_server, wasmo_path, wasmo_client_id, wasmo_client_secret
629631
);
630632
let content_at_default_path = format!("WASMO_PATH={}", wasmo_path);
631633

@@ -686,17 +688,18 @@ async fn main() {
686688
server,
687689
path,
688690
host,
689-
token
691+
client_id,
692+
client_secret
690693
} => {
691-
build(path.map(absolute_path), server, Host::from_str(&host).unwrap(), token).await
694+
build(path.map(absolute_path), server, Host::from_str(&host).unwrap(), client_id, client_secret).await
692695
},
693696
Commands::Config { command } => match command {
694697
ConfigCommands::Set {
695-
token,
698+
client_id,
699+
client_secret,
696700
server,
697-
path,
698-
docker_authorization
699-
} => set_configuration(token, server, path.map(absolute_path), docker_authorization),
701+
path
702+
} => set_configuration(server, path.map(absolute_path), client_id, client_secret),
700703
ConfigCommands::Get {} => {
701704
logger::loading("<yellow>Read</> configuration".to_string());
702705
let configuration = read_configuration().unwrap();
@@ -713,4 +716,6 @@ async fn main() {
713716
logger::error(format!("{}", e));
714717
std::process::exit(1);
715718
}
719+
720+
std::process::exit(0);
716721
}

0 commit comments

Comments
 (0)