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
|
diff -rNu buildbot/secrets.orig/providers/__init__.py buildbot/secrets/providers/__init__.py
--- buildbot/secrets.orig/providers/__init__.py 1970-01-01 01:00:00.000000000 +0100
+++ buildbot/secrets/providers/__init__.py 2017-03-28 21:52:03.947803965 +0200
@@ -0,0 +1,14 @@
+# This file is part of Buildbot. Buildbot is free software: you can
+# redistribute it and/or modify it under the terms of the GNU General Public
+# License as published by the Free Software Foundation, version 2.
+#
+# This program is distributed in the hope that it will be useful, but WITHOUT
+# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
+# details.
+#
+# You should have received a copy of the GNU General Public License along with
+# this program; if not, write to the Free Software Foundation, Inc., 51
+# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+#
+# Copyright Buildbot Team Members
diff -rNu buildbot/secrets.orig/providers/base.py buildbot/secrets/providers/base.py
--- buildbot/secrets.orig/providers/base.py 1970-01-01 01:00:00.000000000 +0100
+++ buildbot/secrets/providers/base.py 2017-03-28 21:52:03.947803965 +0200
@@ -0,0 +1,35 @@
+# This file is part of Buildbot. Buildbot is free software: you can
+# redistribute it and/or modify it under the terms of the GNU General Public
+# License as published by the Free Software Foundation, version 2.
+#
+# This program is distributed in the hope that it will be useful, but WITHOUT
+# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
+# details.
+#
+# You should have received a copy of the GNU General Public License along with
+# this program; if not, write to the Free Software Foundation, Inc., 51
+# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+#
+# Copyright Buildbot Team Members
+"""
+secret provider interface
+"""
+from __future__ import absolute_import
+from __future__ import print_function
+
+import abc
+
+from buildbot.util.service import BuildbotService
+
+
+class SecretProviderBase(BuildbotService):
+ """
+ Secret provider base
+ """
+
+ @abc.abstractmethod
+ def get(self, *args, **kwargs):
+ """
+ this should be an abstract method
+ """
diff -rNu buildbot/secrets.orig/providers/file.py buildbot/secrets/providers/file.py
--- buildbot/secrets.orig/providers/file.py 1970-01-01 01:00:00.000000000 +0100
+++ buildbot/secrets/providers/file.py 2017-03-28 21:52:03.947803965 +0200
@@ -0,0 +1,82 @@
+# This file is part of Buildbot. Buildbot is free software: you can
+# redistribute it and/or modify it under the terms of the GNU General Public
+# License as published by the Free Software Foundation, version 2.
+#
+# This program is distributed in the hope that it will be useful, but WITHOUT
+# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
+# details.
+#
+# You should have received a copy of the GNU General Public License along with
+# this program; if not, write to the Free Software Foundation, Inc., 51
+# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+#
+# Copyright Buildbot Team Members
+"""
+file based provider
+"""
+from __future__ import absolute_import
+from __future__ import print_function
+
+import os
+import stat
+
+from buildbot import config
+from buildbot.secrets.providers.base import SecretProviderBase
+
+
+class SecretInAFile(SecretProviderBase):
+ """
+ secret is stored in a separate file under the given directory name
+ """
+ name = "SecretInAFile"
+
+ def checkFileIsReadOnly(self, dirname, secretfile):
+ filepath = os.path.join(dirname, secretfile)
+ obs_stat = stat.S_IMODE(os.stat(filepath).st_mode)
+ if (obs_stat & 0o77) != 0 and os.name == "posix":
+ config.error("Permissions %s on file %s are too open."
+ " It is required that your secret files are NOT"
+ " accessible by others!" % (oct(obs_stat),
+ secretfile))
+
+ def checkSecretDirectoryIsAvailableAndReadable(self, dirname, suffixes):
+ if not os.access(dirname, os.F_OK):
+ config.error("directory %s does not exists" % dirname)
+ for secretfile in os.listdir(dirname):
+ for suffix in suffixes:
+ if secretfile.endswith(suffix):
+ self.checkFileIsReadOnly(dirname, secretfile)
+
+ def loadSecrets(self, dirname, suffixes):
+ secrets = {}
+ for secretfile in os.listdir(dirname):
+ secretvalue = None
+ for suffix in suffixes:
+ if secretfile.endswith(suffix):
+ with open(os.path.join(dirname, secretfile)) as source:
+ secretvalue = source.read()
+ if suffix:
+ secretfile = secretfile[:-len(suffix)]
+ secrets[secretfile] = secretvalue
+ return secrets
+
+ def checkConfig(self, dirname, suffixes=None):
+ self._dirname = dirname
+ if suffixes is None:
+ suffixes = [""]
+ self.checkSecretDirectoryIsAvailableAndReadable(dirname,
+ suffixes=suffixes)
+
+ def reconfigService(self, dirname, suffixes=None):
+ self._dirname = dirname
+ self.secrets = {}
+ if suffixes is None:
+ suffixes = [""]
+ self.secrets = self.loadSecrets(self._dirname, suffixes=suffixes)
+
+ def get(self, entry):
+ """
+ get the value from the file identified by 'entry'
+ """
+ return self.secrets.get(entry)
diff -rNu buildbot/secrets.orig/providers/vault.py buildbot/secrets/providers/vault.py
--- buildbot/secrets.orig/providers/vault.py 1970-01-01 01:00:00.000000000 +0100
+++ buildbot/secrets/providers/vault.py 2017-03-28 21:52:03.947803965 +0200
@@ -0,0 +1,67 @@
+# This file is part of Buildbot. Buildbot is free software: you can
+# redistribute it and/or modify it under the terms of the GNU General Public
+# License as published by the Free Software Foundation, version 2.
+#
+# This program is distributed in the hope that it will be useful, but WITHOUT
+# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
+# details.
+#
+# You should have received a copy of the GNU General Public License along with
+# this program; if not, write to the Free Software Foundation, Inc., 51
+# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+#
+# Copyright Buildbot Team Members
+"""
+vault based providers
+"""
+
+from __future__ import absolute_import
+from __future__ import print_function
+
+from twisted.internet import defer
+
+from buildbot import config
+from buildbot.secrets.providers.base import SecretProviderBase
+from buildbot.util import httpclientservice
+
+
+class HashiCorpVaultSecretProvider(SecretProviderBase):
+ """
+ basic provider where each secret is stored in Vault
+ """
+
+ name = 'SecretInVault'
+
+ def checkConfig(self, vaultServer=None, vaultToken=None, secretsmount=None):
+ if not isinstance(vaultServer, str):
+ config.error("vaultServer must be a string while it is %s" % (type(vaultServer,)))
+ if not isinstance(vaultToken, str):
+ config.error("vaultToken must be a string while it is %s" % (type(vaultToken,)))
+
+ @defer.inlineCallbacks
+ def reconfigService(self, vaultServer=None, vaultToken=None, secretsmount=None):
+ if secretsmount is None:
+ self.secretsmount = "secret"
+ else:
+ self.secretsmount = secretsmount
+ self.vaultServer = vaultServer
+ self.vaultToken = vaultToken
+ if vaultServer.endswith('/'):
+ vaultServer = vaultServer[:-1]
+ self._http = yield httpclientservice.HTTPClientService.getService(
+ self.master, self.vaultServer, headers={'X-Vault-Token': self.vaultToken})
+
+ @defer.inlineCallbacks
+ def get(self, entry):
+ """
+ get the value from vault secret backend
+ """
+ path = self.secretsmount + '/' + entry
+ proj = yield self._http.get('/v1/{0}'.format(path))
+ code = yield proj.code
+ if code != 200:
+ raise KeyError("The key %s does not exist in Vault provider: request"
+ " return code:%d." % (entry, code))
+ json = yield proj.json()
+ defer.returnValue(json.get(u'data', {}).get('value'))
|