summaryrefslogtreecommitdiff
path: root/access-secret
blob: 993278dd64ec5a9c1724d5c7cae82643bc391c18 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#!/usr/bin/python

import os
import sys
import getopt
import yaml
import gnupg

config_path = os.path.expanduser("~")+"/.config/access-secret.yaml"

text_red = '\033[31m'
text_blue = '\033[34m'
text_green = '\033[32m'
text_cyan = '\033[36m'
text_bold = '\033[1m'
text_reset = '\033[0m'

class access_secret():
	entries = []
	config = {}

	# secret

	def secret_init(self):
		self.gpg = gnupg.GPG()

	def secret_path(self, secret):
		return os.path.join(self.config["path"]["secrets"], secret)

	def secret_load(self, secret):
		path = self.secret_path(secret)

		s = open(path, "rb")
		d = self.gpg.decrypt_file(s)
		s.close()

		return str(d)

	def secret_store(self, secret, data):
		path = self.secret_path(secret)

		d = self.gpg.encrypt(data, self.config["gpg"]["recipient"])

		s = open(path, "w")
		s.write(str(d))
		s.close()

	def secret_add(self, secret):
		data = input("Secret for "+secret+": ")
		secret += ".asc"
		self.secret_store(secret, data)

		print("Secret stored in "+secret)

	# entry

	def entry_load_path(self, path):
		s = open(path, "r")
		y = yaml.load(s, Loader = yaml.SafeLoader)
		s.close()

		for e in y:
			self.entries.append(e)

	def entry_show(self, entry, secret_show = False):
		name = entry["name"]
		secret = entry["secret"]

		if secret_show:
			name_head = ""
			data_head = "- "
		else:
			name_head = "- "

		print(text_bold+name_head+name+text_reset, end = "")

		if "address" in entry:
			address = entry["address"]

			if type(address) is list:
				separator = ""

				print(" (", end = "")

				for address_entry in address:
					print(separator+address_entry, end = "")
					separator = ", "

				print(")", end = "")
			else:
				print(" ("+address+")", end = "")

		if secret_show:
			data = self.secret_load(secret)

			print(":")

			for line in data.split("\n"):
				if line == "":
					continue

				print(data_head+line)
		else:
			print("")

	# category

	def category_name(self, label):
		if label in self.config["categories"]:
			return self.config["categories"][label]

		return label.capitalize()

	# group

	def group_name(self, label):
		if label in self.config["groups"]:
			return self.config["groups"][label]

		return label.capitalize()

	# entries

	def entries_sort(self):
		entries = self.entries

		entries = sorted(entries, key = lambda entry: entry["group"])
		entries = sorted(entries, key = lambda entry: entry["category"])

		self.entries = entries

	def entries_match(self, match):
		match = match.lower()
		entries = []

		for entry in self.entries:
			name = entry["name"].lower()
			category = entry["category"].lower()
			group = entry["group"].lower()

			if match in name or match in category or match in group:
				entries.append(entry)
			elif "address" in entry:
				address = entry["address"]

				if type(address) is list:
					for address_entry in address:
						if match in address_entry:
							entries.append(entry)
				else:
					if match in address:
						entries.append(entry)

		return entries

	def entries_list(self, entries):
		separator = ""
		category = ""
		group = ""

		for entry in entries:
			clear = False

			if entry["category"] != category:
				category = entry["category"]
				name = self.category_name(category)

				print(text_blue+text_bold+separator+"# "+name+text_reset)

				clear = True
				separator = "\n"

			if entry["group"] != group:
				group = entry["group"]
				name = self.group_name(group)

				print(text_green+text_bold+separator+"## "+name+text_reset)

				clear = True
				separator = "\n"

			if clear:
				print(separator, end = "")

			self.entry_show(entry, False)

	def entries_show(self, entries):

		for entry in entries:
			self.entry_show(entry, True)

			if entry != entries[-1]:
				print("")

	def entries_load(self):
		base_path = self.config["path"]["base"]
		entries_paths = [ os.path.join(base_path, f) for f in os.listdir(base_path) if f.endswith(".yaml") ]

		for entry_path in entries_paths:
			self.entry_load_path(entry_path)

	# config

	def config_load(self):
		s = open(config_path, "r")
		y = yaml.load(s, Loader = yaml.SafeLoader)
		s.close()

		self.config = y

	# main

	def main(self):
		secret_add = None

		self.config_load()
		self.secret_init()
		self.entries_load()
		self.entries_sort()

		opts, args = getopt.getopt(sys.argv[1:], "a:")

		for key, value in opts:
			if key == "-a":
				secret_add = value

		if secret_add:
			self.secret_add(secret_add)
		elif len(args) == 1:
			entries = self.entries_match(args[0])
			self.entries_show(entries)
		else:
			self.entries_list(self.entries)

if __name__ == "__main__":
	access_secret().main()