Skip to content

Commit 0d11056

Browse files
committed
initial commit
0 parents  commit 0d11056

File tree

5 files changed

+109
-0
lines changed

5 files changed

+109
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
config.edn
2+
.idea/

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2020 nextjournal
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# SSH Auth Github
2+
3+
This is a small [Babashka](https://github.com/borkdude/babashka/) script that retrieves public keys from a specific team under a specific organization on Github.
4+
5+
You need a `config.edn` next to the script with
6+
7+
```clojure
8+
{:token ""
9+
:organization ""
10+
:team ""}
11+
12+
```
13+
14+
Keep in mind that the token **must have** the following permissions: `read:org`, `read:user`, `user:email`
15+
16+
17+
# Acknowledgements
18+
19+
This library was inspired by the [Rust analogous](https://github.com/dalen/ssh-auth-github)

config.edn.template

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{:token ""
2+
:organization ""
3+
:team ""}

ssh-auth-github

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#! /usr/bin/env bb
2+
3+
(require '[babashka.curl :as curl])
4+
(require '[cheshire.core :as json])
5+
6+
(defn read-config []
7+
(read-string (slurp "config.edn")))
8+
9+
(defn query [organization team]
10+
(format "{organization(login: \"%s\") {
11+
team(slug: \"%s\") {
12+
members {
13+
nodes {
14+
login
15+
publicKeys(first: 100) {
16+
nodes {
17+
key
18+
}
19+
}
20+
}
21+
}
22+
}
23+
}
24+
}"
25+
organization team))
26+
27+
28+
(defn retrieve-keys [config]
29+
(let [resp (curl/post "https://api.github.com/graphql"
30+
{:body (json/generate-string {"query" (query (:organization config)
31+
(:team config))})
32+
:throw false
33+
:headers {"Authorization" (format "bearer %s" (:token config))}})
34+
body (-> resp
35+
(:body)
36+
(json/parse-string true))]
37+
38+
(cond
39+
(not= 200 (:status resp))
40+
(do (clojure.pprint/pprint body)
41+
(System/exit 1))
42+
43+
(:errors body)
44+
(do
45+
(clojure.pprint/pprint (:errors body))
46+
(System/exit 1))
47+
48+
:else
49+
body)))
50+
51+
52+
(defn parse-response [data]
53+
(when-not (seq (get-in data [:data :organization :team]))
54+
(println "Error: Team is empty!")
55+
(System/exit 1))
56+
(->> (get-in data [:data :organization :team :members :nodes])
57+
(mapcat (fn [n] (map #(str (:key %) " " (:login n))
58+
(get-in n [:publicKeys :nodes])))))
59+
)
60+
61+
(doseq [l (->> (read-config)
62+
(retrieve-keys)
63+
(parse-response))]
64+
(println l))

0 commit comments

Comments
 (0)