From 70fb42a677d73c439901e3d33624df2f1f62e8a7 Mon Sep 17 00:00:00 2001 From: Antoine Date: Wed, 16 Sep 2020 19:49:19 +0200 Subject: [PATCH] implement gitlab-runner verify --delete --- doc-gitlab-runner.md | 103 ------------------ misc/doc-gitlab-runner.md | 35 ++++++ issue.md => misc/issue_graalvm.md | 0 misc/requests/post_job_request.txt | 37 +++++++ misc/requests/post_runner_register.txt | 40 +++++++ misc/requests/post_runner_verify.txt | 8 ++ .../antoine_roux/wiki/ControllerHandlers.java | 21 +++- .../tk/antoine_roux/wiki/RunnerRegistrar.java | 8 +- .../tk/antoine_roux/wiki/model/Runner.java | 6 +- .../{DeleteRunner.java => TokenRunner.java} | 2 +- ...ponse.java => RegisterRunnerResponse.java} | 4 +- 11 files changed, 148 insertions(+), 116 deletions(-) delete mode 100644 doc-gitlab-runner.md create mode 100644 misc/doc-gitlab-runner.md rename issue.md => misc/issue_graalvm.md (100%) create mode 100644 misc/requests/post_job_request.txt create mode 100644 misc/requests/post_runner_register.txt create mode 100644 misc/requests/post_runner_verify.txt rename src/main/java/tk/antoine_roux/wiki/model/request/{DeleteRunner.java => TokenRunner.java} (83%) rename src/main/java/tk/antoine_roux/wiki/model/response/{RegisterResponse.java => RegisterRunnerResponse.java} (78%) diff --git a/doc-gitlab-runner.md b/doc-gitlab-runner.md deleted file mode 100644 index 0e5f64e..0000000 --- a/doc-gitlab-runner.md +++ /dev/null @@ -1,103 +0,0 @@ -# gitlab runner - -command used to register new runner - -```shell script -$ docker run -d --name gitlab-runner --restart always -v /var/run/docker.sock:/var/run/docker.sock -v gitlab-runner-config:/etc/gitlab-runner gitlab/gitlab-runner:latest - -$ gitlab-runner register -non-interactive --description "manualy registered gitlab runner" --url "http://172.17.0.1:8080/" --registration-token "3b79eb1f-32f3-4db2-ad1b-6 - 702e476d839" --tag-list "docker,manual" --executor shell -``` - -same with curl - -```shell script -$ curl --request POST -H 'Content-Type: application/json' "http://localhost:8080/api/v4/runners" -d '{"description":"manualy registered gitlab runner","tag_list":"docker,manual","run_untagged":false,"locked":true,"active":true,"info":{"name":"gitlab-runner","version":"13.3.1","revision":"738bbe5a","platform":"linux","architecture":"amd64","features":{"variables":false,"image":false,"services":false,"artifacts":false,"cache":false,"shared":false,"upload_multiple_artifacts":false,"upload_raw_artifacts":false,"session":false,"terminal":false,"refspecs":false,"masking":false,"proxy":false,"raw_variables":false,"artifacts_exclude":false,"multi_build_steps":false}},"token":"registration_token"}' -``` - - - ---- - -```json -{ - "description":"manualy registered gitlab runner", - "tag_list":"docker,manual", - "run_untagged":false, - "locked":true, - "active":true, - "info":{ - "name":"gitlab-runner", - "version":"13.3.1", - "revision":"738bbe5a", - "platform":"linux", - "architecture":"amd64", - "features":{ - "variables":false, - "image":false, - "services":false, - "artifacts":false, - "cache":false, - "shared":false, - "upload_multiple_artifacts":false, - "upload_raw_artifacts":false, - "session":false, - "terminal":false, - "refspecs":false, - "masking":false, - "proxy":false, - "raw_variables":false, - "artifacts_exclude":false, - "multi_build_steps":false - } - }, - "token":"registration_token" -} -``` - -## http call use when job is run - -``` -[POST /api/v4/jobs/request HTTP/1.1 -Host: 172.17.0.1:8080 -User-Agent: gitlab-runner 13.3.1 (13-3-stable; go1.13.8; linux/amd64) -Content-Length: 510 -Accept: application/json -Content-Type: application/json -Accept-Encoding: gzip -``` - -```json -{ - "info":{ - "name":"gitlab-runner", - "version":"13.3.1", - "revision":"738bbe5a", - "platform":"linux", - "architecture":"amd64", - "executor":"shell", - "shell":"bash", - "features":{ - "variables":true, - "image":false, - "services":false, - "artifacts":true, - "cache":true, - "shared":true, - "upload_multiple_artifacts":true, - "upload_raw_artifacts":true, - "session":true, - "terminal":true, - "refspecs":true, - "masking":true, - "proxy":false, - "raw_variables":true, - "artifacts_exclude":true, - "multi_build_steps":true - } - }, - "token":"76a79b73-b211-48c6-a3da-6b99fb8b0612" -} -``` - -... diff --git a/misc/doc-gitlab-runner.md b/misc/doc-gitlab-runner.md new file mode 100644 index 0000000..d6159d9 --- /dev/null +++ b/misc/doc-gitlab-runner.md @@ -0,0 +1,35 @@ +# gitlab runner + +## start gitlab runner + +```shell script +$ docker run -d --name gitlab-runner --restart always -v /var/run/docker.sock:/var/run/docker.sock -v gitlab-runner-config:/etc/gitlab-runner gitlab/gitlab-runner:latest +``` + +## register new runner + +```shell script +$ gitlab-runner register -non-interactive --description "manualy registered gitlab runner" --url "http://172.17.0.1:8080/" --registration-token "3b79eb1f-32f3-4db2-ad1b-6 + 702e476d839" --tag-list "docker,manual" --executor shell +``` + +with curl + +```shell script +$ curl --request POST -H 'Content-Type: application/json' "http://localhost:8080/api/v4/runners" -d '{"description":"manualy registered gitlab runner","tag_list":"docker,manual","run_untagged":false,"locked":true,"active":true,"info":{"name":"gitlab-runner","version":"13.3.1","revision":"738bbe5a","platform":"linux","architecture":"amd64","features":{"variables":false,"image":false,"services":false,"artifacts":false,"cache":false,"shared":false,"upload_multiple_artifacts":false,"upload_raw_artifacts":false,"session":false,"terminal":false,"refspecs":false,"masking":false,"proxy":false,"raw_variables":false,"artifacts_exclude":false,"multi_build_steps":false}},"token":"registration_token"}' +``` + +## list runners + +```shell script +$ gitlab-runner list +``` +local listing : + +cf => /etc/gitlab-runner/config.toml + +## verify which runner is always alive + +```shell script +$ gitlab-runner verify --delete +``` diff --git a/issue.md b/misc/issue_graalvm.md similarity index 100% rename from issue.md rename to misc/issue_graalvm.md diff --git a/misc/requests/post_job_request.txt b/misc/requests/post_job_request.txt new file mode 100644 index 0000000..4cbfb24 --- /dev/null +++ b/misc/requests/post_job_request.txt @@ -0,0 +1,37 @@ +[POST /api/v4/jobs/request HTTP/1.1 +Host: 172.17.0.1:8080 +User-Agent: gitlab-runner 13.3.1 (13-3-stable; go1.13.8; linux/amd64) +Content-Length: 510 +Accept: application/json +Content-Type: application/json +Accept-Encoding: gzip] +{ + "info":{ + "name":"gitlab-runner", + "version":"13.3.1", + "revision":"738bbe5a", + "platform":"linux", + "architecture":"amd64", + "executor":"shell", + "shell":"bash", + "features":{ + "variables":true, + "image":false, + "services":false, + "artifacts":true, + "cache":true, + "shared":true, + "upload_multiple_artifacts":true, + "upload_raw_artifacts":true, + "session":true, + "terminal":true, + "refspecs":true, + "masking":true, + "proxy":false, + "raw_variables":true, + "artifacts_exclude":true, + "multi_build_steps":true + } + }, + "token":"76a79b73-b211-48c6-a3da-6b99fb8b0612" +} diff --git a/misc/requests/post_runner_register.txt b/misc/requests/post_runner_register.txt new file mode 100644 index 0000000..d7dda53 --- /dev/null +++ b/misc/requests/post_runner_register.txt @@ -0,0 +1,40 @@ +[POST /api/v4/runners HTTP/1.1 + Host: 172.17.0.1:8080 + User-Agent: gitlab-runner 13.3.1 (13-3-stable; go1.13.8; linux/amd64) + Content-Length: 510 + Accept: application/json + Content-Type: application/json + Accept-Encoding: gzip] +{ + "description": "manualy registered gitlab runner", + "tag_list": "docker,manual", + "run_untagged": false, + "locked": true, + "active": true, + "info": { + "name": "gitlab-runner", + "version": "13.3.1", + "revision": "738bbe5a", + "platform": "linux", + "architecture": "amd64", + "features": { + "variables": false, + "image": false, + "services": false, + "artifacts": false, + "cache": false, + "shared": false, + "upload_multiple_artifacts": false, + "upload_raw_artifacts": false, + "session": false, + "terminal": false, + "refspecs": false, + "masking": false, + "proxy": false, + "raw_variables": false, + "artifacts_exclude": false, + "multi_build_steps": false + } + }, + "token": "registration_token" +} diff --git a/misc/requests/post_runner_verify.txt b/misc/requests/post_runner_verify.txt new file mode 100644 index 0000000..5b1c3d8 --- /dev/null +++ b/misc/requests/post_runner_verify.txt @@ -0,0 +1,8 @@ +[POST /api/v4/runners/verify HTTP/1.1 +Host: 172.17.0.1:8080 +User-Agent: gitlab-runner 13.3.1 (13-3-stable; go1.13.8; linux/amd64) +Content-Length: 48 +Content-Type: application/json +Accept-Encoding: gzip] + +{"token":"19b280e8-9c56-4566-8234-7a172d717be3"} diff --git a/src/main/java/tk/antoine_roux/wiki/ControllerHandlers.java b/src/main/java/tk/antoine_roux/wiki/ControllerHandlers.java index ed4d004..828c78d 100644 --- a/src/main/java/tk/antoine_roux/wiki/ControllerHandlers.java +++ b/src/main/java/tk/antoine_roux/wiki/ControllerHandlers.java @@ -11,8 +11,8 @@ import tk.antoine_roux.wiki.annotation.ApiVersion; import tk.antoine_roux.wiki.configuration.Exception.DeleteRunnerException; import tk.antoine_roux.wiki.model.Runner; import tk.antoine_roux.wiki.model.request.AddRunner; -import tk.antoine_roux.wiki.model.request.DeleteRunner; -import tk.antoine_roux.wiki.model.response.RegisterResponse; +import tk.antoine_roux.wiki.model.request.TokenRunner; +import tk.antoine_roux.wiki.model.response.RegisterRunnerResponse; import tk.antoine_roux.wiki.utilitary.Boolean; import java.lang.invoke.MethodHandles; @@ -62,22 +62,33 @@ public class ControllerHandlers { @ResponseBody @ApiVersion({API_VERSION}) @PostMapping("/runners") - public ResponseEntity addRunner(@RequestBody AddRunner body) { + public ResponseEntity addRunner(@RequestBody AddRunner body) { logger.debug("Receive register runner request " + body); Runner createdRunner = this.runnerRegistrar.addRunner(body); - RegisterResponse response = createdRunner.toRegisterResponse(); + RegisterRunnerResponse response = createdRunner.toRegisterResponse(); logger.debug("Response register runner " + response); return ResponseEntity.status(HttpStatus.CREATED).body(response); } @ApiVersion(API_VERSION) @DeleteMapping("/runners") - public ResponseEntity deleteRunner(@RequestBody DeleteRunner body) throws Exception { + public ResponseEntity deleteRunner(@RequestBody TokenRunner body) throws Exception { logger.debug("Receive delete runner request " + body); Boolean.trueOrElseThrow(this.runnerRegistrar.removeRunnerByRegistrationToken(body), DeleteRunnerException::new); logger.debug("Successfully delete runner "); return ResponseEntity.noContent().build(); } + + @ApiVersion(API_VERSION) + @PostMapping("/runners/verify") + public ResponseEntity runnerExist(@RequestBody TokenRunner body) { + boolean isValid = this.runnerRegistrar.checkRunner(body); + if (isValid) { + return ResponseEntity.ok().build(); + } else { + return ResponseEntity.status(HttpStatus.FORBIDDEN).build(); + } + } } diff --git a/src/main/java/tk/antoine_roux/wiki/RunnerRegistrar.java b/src/main/java/tk/antoine_roux/wiki/RunnerRegistrar.java index 6edd47f..eed90a4 100644 --- a/src/main/java/tk/antoine_roux/wiki/RunnerRegistrar.java +++ b/src/main/java/tk/antoine_roux/wiki/RunnerRegistrar.java @@ -3,7 +3,7 @@ package tk.antoine_roux.wiki; import org.springframework.stereotype.Service; import tk.antoine_roux.wiki.model.Runner; import tk.antoine_roux.wiki.model.request.AddRunner; -import tk.antoine_roux.wiki.model.request.DeleteRunner; +import tk.antoine_roux.wiki.model.request.TokenRunner; import java.util.ArrayList; import java.util.List; @@ -34,7 +34,11 @@ public class RunnerRegistrar { return r; } - public boolean removeRunnerByRegistrationToken(DeleteRunner r) { + public boolean removeRunnerByRegistrationToken(TokenRunner r) { return this.runners.removeIf(runner -> runner.authenticationToken.equals(r.token)); } + + public boolean checkRunner(TokenRunner body) { + return this.runners.stream().anyMatch(runner -> runner.authenticationToken.equals(body.token)); + } } diff --git a/src/main/java/tk/antoine_roux/wiki/model/Runner.java b/src/main/java/tk/antoine_roux/wiki/model/Runner.java index ca3098d..49b67e9 100644 --- a/src/main/java/tk/antoine_roux/wiki/model/Runner.java +++ b/src/main/java/tk/antoine_roux/wiki/model/Runner.java @@ -1,6 +1,6 @@ package tk.antoine_roux.wiki.model; -import tk.antoine_roux.wiki.model.response.RegisterResponse; +import tk.antoine_roux.wiki.model.response.RegisterRunnerResponse; import java.util.List; import java.util.UUID; @@ -15,7 +15,7 @@ public class Runner { public UUID authenticationToken; public Integer id; - public RegisterResponse toRegisterResponse() { - return new RegisterResponse(String.valueOf(this.id), this.authenticationToken.toString()); + public RegisterRunnerResponse toRegisterResponse() { + return new RegisterRunnerResponse(String.valueOf(this.id), this.authenticationToken.toString()); } } diff --git a/src/main/java/tk/antoine_roux/wiki/model/request/DeleteRunner.java b/src/main/java/tk/antoine_roux/wiki/model/request/TokenRunner.java similarity index 83% rename from src/main/java/tk/antoine_roux/wiki/model/request/DeleteRunner.java rename to src/main/java/tk/antoine_roux/wiki/model/request/TokenRunner.java index 426afd0..42a8b17 100644 --- a/src/main/java/tk/antoine_roux/wiki/model/request/DeleteRunner.java +++ b/src/main/java/tk/antoine_roux/wiki/model/request/TokenRunner.java @@ -5,6 +5,6 @@ import java.util.UUID; /** * Model use to remove runner instance */ -public class DeleteRunner { +public class TokenRunner { public UUID token; } diff --git a/src/main/java/tk/antoine_roux/wiki/model/response/RegisterResponse.java b/src/main/java/tk/antoine_roux/wiki/model/response/RegisterRunnerResponse.java similarity index 78% rename from src/main/java/tk/antoine_roux/wiki/model/response/RegisterResponse.java rename to src/main/java/tk/antoine_roux/wiki/model/response/RegisterRunnerResponse.java index 59dedb4..2976abd 100644 --- a/src/main/java/tk/antoine_roux/wiki/model/response/RegisterResponse.java +++ b/src/main/java/tk/antoine_roux/wiki/model/response/RegisterRunnerResponse.java @@ -5,13 +5,13 @@ import com.fasterxml.jackson.annotation.JsonProperty; /** * Json response object for register endpoint */ -public class RegisterResponse { +public class RegisterRunnerResponse { @JsonProperty(access = JsonProperty.Access.READ_ONLY) String id; @JsonProperty(access = JsonProperty.Access.READ_ONLY) String token; - public RegisterResponse(String id, String token) { + public RegisterRunnerResponse(String id, String token) { this.id = id; this.token = token; }