Title: [285900] trunk/Tools
Revision
285900
Author
jbed...@apple.com
Date
2021-11-16 16:56:19 -0800 (Tue, 16 Nov 2021)

Log Message

[webkitscmpy] Load secrets in kubernetes
https://bugs.webkit.org/show_bug.cgi?id=233158
<rdar://problem/85433499>

Reviewed by Dewei Zhu.

* Tools/Scripts/libraries/webkitcorepy/setup.py: Bump version.
* Tools/Scripts/libraries/webkitcorepy/webkitcorepy/__init__.py: Bump version, export Environment.
* Tools/Scripts/libraries/webkitcorepy/webkitcorepy/credentials.py:
(credentials): Use Environment instead of os.environ.
* Tools/Scripts/libraries/webkitcorepy/webkitcorepy/environment.py: Added.
(Environment):
(Environment.instance): Construct a shared Environmant intance.
(Environment.__init__):
(Environment.load): Load secrets in file directory.
(Environment.get): Check environment variable and then secret store.
(Environment.__getitem__):
(Environment.__setitem__):
(Environment.keys): Iterate through secret store and environment variables.
(Environment.values): Ditto.
(Environment.items): Ditto.
* Tools/Scripts/libraries/webkitcorepy/webkitcorepy/tests/environment_unittest.py: Added.
(TestEnvironment):
(TestEnvironment.test_basic):
(TestEnvironment.test_scoped):
(TestEnvironment.test_list):

Canonical link: https://commits.webkit.org/244316@main

Modified Paths

Added Paths

Diff

Modified: trunk/Tools/ChangeLog (285899 => 285900)


--- trunk/Tools/ChangeLog	2021-11-17 00:31:49 UTC (rev 285899)
+++ trunk/Tools/ChangeLog	2021-11-17 00:56:19 UTC (rev 285900)
@@ -1,3 +1,32 @@
+2021-11-16  Jonathan Bedard  <jbed...@apple.com>
+
+        [webkitscmpy] Load secrets in kubernetes
+        https://bugs.webkit.org/show_bug.cgi?id=233158
+        <rdar://problem/85433499>
+
+        Reviewed by Dewei Zhu.
+
+        * Scripts/libraries/webkitcorepy/setup.py: Bump version.
+        * Scripts/libraries/webkitcorepy/webkitcorepy/__init__.py: Bump version, export Environment.
+        * Scripts/libraries/webkitcorepy/webkitcorepy/credentials.py:
+        (credentials): Use Environment instead of os.environ.
+        * Scripts/libraries/webkitcorepy/webkitcorepy/environment.py: Added.
+        (Environment):
+        (Environment.instance): Construct a shared Environmant intance.
+        (Environment.__init__):
+        (Environment.load): Load secrets in file directory.
+        (Environment.get): Check environment variable and then secret store.
+        (Environment.__getitem__):
+        (Environment.__setitem__):
+        (Environment.keys): Iterate through secret store and environment variables.
+        (Environment.values): Ditto.
+        (Environment.items): Ditto.
+        * Scripts/libraries/webkitcorepy/webkitcorepy/tests/environment_unittest.py: Added.
+        (TestEnvironment):
+        (TestEnvironment.test_basic):
+        (TestEnvironment.test_scoped):
+        (TestEnvironment.test_list):
+
 2021-11-12  Jonathan Bedard  <jbed...@apple.com>
 
         [git-webkit] Mark landed changes as merged

Modified: trunk/Tools/Scripts/libraries/webkitcorepy/setup.py (285899 => 285900)


--- trunk/Tools/Scripts/libraries/webkitcorepy/setup.py	2021-11-17 00:31:49 UTC (rev 285899)
+++ trunk/Tools/Scripts/libraries/webkitcorepy/setup.py	2021-11-17 00:56:19 UTC (rev 285900)
@@ -30,7 +30,7 @@
 
 setup(
     name='webkitcorepy',
-    version='0.11.4',
+    version='0.12.0',
     description='Library containing various Python support classes and functions.',
     long_description=readme(),
     classifiers=[

Modified: trunk/Tools/Scripts/libraries/webkitcorepy/webkitcorepy/__init__.py (285899 => 285900)


--- trunk/Tools/Scripts/libraries/webkitcorepy/webkitcorepy/__init__.py	2021-11-17 00:31:49 UTC (rev 285899)
+++ trunk/Tools/Scripts/libraries/webkitcorepy/webkitcorepy/__init__.py	2021-11-17 00:56:19 UTC (rev 285900)
@@ -36,6 +36,7 @@
 from webkitcorepy.output_capture import LoggerCapture, OutputCapture, OutputDuplicate
 from webkitcorepy.task_pool import TaskPool
 from webkitcorepy.terminal import Terminal
+from webkitcorepy.environment import Environment
 from webkitcorepy.credentials import credentials
 from webkitcorepy.measure_time import MeasureTime
 from webkitcorepy.nested_fuzzy_dict import NestedFuzzyDict
@@ -43,7 +44,7 @@
 from webkitcorepy.editor import Editor
 from webkitcorepy.file_lock import FileLock
 
-version = Version(0, 11, 4)
+version = Version(0, 12, 0)
 
 from webkitcorepy.autoinstall import Package, AutoInstall
 if sys.version_info > (3, 0):

Modified: trunk/Tools/Scripts/libraries/webkitcorepy/webkitcorepy/credentials.py (285899 => 285900)


--- trunk/Tools/Scripts/libraries/webkitcorepy/webkitcorepy/credentials.py	2021-11-17 00:31:49 UTC (rev 285899)
+++ trunk/Tools/Scripts/libraries/webkitcorepy/webkitcorepy/credentials.py	2021-11-17 00:56:19 UTC (rev 285900)
@@ -20,12 +20,11 @@
 # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
-import os
 import getpass
 import sys
 
 from subprocess import CalledProcessError
-from webkitcorepy import OutputCapture, Terminal
+from webkitcorepy import Environment, OutputCapture, Terminal
 
 _cache = dict()
 
@@ -37,8 +36,8 @@
     if _cache.get(name):
         return _cache.get(name)
 
-    username = os.environ.get('{}_USERNAME'.format(name.upper()))
-    key = os.environ.get('{}_{}'.format(name.upper(), key_name.upper()))
+    username = Environment.instance().get('{}_USERNAME'.format(name.upper()))
+    key = Environment.instance().get('{}_{}'.format(name.upper(), key_name.upper()))
 
     if username and key:
         _cache[name] = (username, key)

Added: trunk/Tools/Scripts/libraries/webkitcorepy/webkitcorepy/environment.py (0 => 285900)


--- trunk/Tools/Scripts/libraries/webkitcorepy/webkitcorepy/environment.py	                        (rev 0)
+++ trunk/Tools/Scripts/libraries/webkitcorepy/webkitcorepy/environment.py	2021-11-17 00:56:19 UTC (rev 285900)
@@ -0,0 +1,83 @@
+# Copyright (C) 2021 Apple Inc. All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+# 1.  Redistributions of source code must retain the above copyright
+#     notice, this list of conditions and the following disclaimer.
+# 2.  Redistributions in binary form must reproduce the above copyright
+#     notice, this list of conditions and the following disclaimer in the
+#     documentation and/or other materials provided with the distribution.
+#
+# THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS "AS IS" AND
+# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+# DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR
+# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+import os
+
+
+class Environment(object):
+    _instance = None
+
+    @classmethod
+    def instance(cls, path=None):
+        if not cls._instance:
+            cls._instance = cls(path=path)
+        if path and path != cls._instance.path:
+            cls._instance.path = path
+        return cls._instance
+
+    def __init__(self, path=None, divider='___'):
+        self._mapping = dict()
+        self.path = path
+        self._divider = divider
+
+    def load(self, *prefixes):
+        if not self.path:
+            return self
+        for file in os.listdir(self.path):
+            prefix, key = file.split(self._divider, 1) if self._divider in file else (None, file)
+            if prefix and prefix not in prefixes:
+                continue
+            with open(os.path.join(self.path, file), 'r') as fl:
+                self._mapping[key] = fl.read().rstrip('\n')
+        return self
+
+    def get(self, key):
+        if key in os.environ:
+            return os.environ[key]
+        return self._mapping.get(key)
+
+    def __getitem__(self, key):
+        result = self.get(key)
+        if not result:
+            raise KeyError(key)
+        return result
+
+    def __setitem__(self, key, value):
+        os.environ[key] = value
+
+    def keys(self):
+        for key in self._mapping.keys():
+            yield key
+        for key in os.environ.keys():
+            yield key
+
+    def values(self):
+        for value in self._mapping.values():
+            yield value
+        for value in os.environ.values():
+            yield value
+
+    def items(self):
+        for key, value in self._mapping.items():
+            yield key, value
+        for key, value in os.environ.items():
+            yield key, value

Added: trunk/Tools/Scripts/libraries/webkitcorepy/webkitcorepy/tests/environment_unittest.py (0 => 285900)


--- trunk/Tools/Scripts/libraries/webkitcorepy/webkitcorepy/tests/environment_unittest.py	                        (rev 0)
+++ trunk/Tools/Scripts/libraries/webkitcorepy/webkitcorepy/tests/environment_unittest.py	2021-11-17 00:56:19 UTC (rev 285900)
@@ -0,0 +1,74 @@
+# Copyright (C) 2020 Apple Inc. All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions
+# are met:
+# 1.  Redistributions of source code must retain the above copyright
+#     notice, this list of conditions and the following disclaimer.
+# 2.  Redistributions in binary form must reproduce the above copyright
+#     notice, this list of conditions and the following disclaimer in the
+#     documentation and/or other materials provided with the distribution.
+#
+# THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS "AS IS" AND
+# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+# DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR
+# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+import os
+
+from webkitcorepy import testing, Environment
+
+
+class TestEnvironment(testing.PathTestCase):
+    basepath = 'mock/secrets'
+
+    def test_basic(self):
+        try:
+            with open(os.path.join(self.path, 'KEY'), 'w') as file:
+                file.write('value\n')
+            self.assertEqual(Environment.instance(self.path).load()['KEY'], 'value')
+        finally:
+            Environment._instance = None
+
+    def test_scoped(self):
+        try:
+            with open(os.path.join(self.path, 'KEY'), 'w') as file:
+                file.write('value ')
+            with open(os.path.join(self.path, 'scope___KEY_A'), 'w') as file:
+                file.write('value_a')
+            with open(os.path.join(self.path, 'other___KEY_B'), 'w') as file:
+                file.write('value_b')
+            self.assertEqual(Environment.instance(self.path).load('scope')['KEY'], 'value ')
+            self.assertEqual(Environment.instance(self.path).load('scope')['KEY_A'], 'value_a')
+            self.assertIsNone(Environment.instance(self.path).load('scope').get('KEY_B'))
+        finally:
+            Environment._instance = None
+
+    def test_list(self):
+        try:
+            with open(os.path.join(self.path, 'KEY_A'), 'w') as file:
+                file.write('value_a')
+            with open(os.path.join(self.path, 'KEY_B'), 'w') as file:
+                file.write('value_b')
+
+            Environment.instance(self.path).load()
+            self.assertEqual(
+                list(Environment.instance(self.path).keys()),
+                ['KEY_A', 'KEY_B'] + list(os.environ.keys()),
+            )
+            self.assertEqual(
+                list(Environment.instance(self.path).values()),
+                ['value_a', 'value_b'] + list(os.environ.values()),
+            )
+            self.assertEqual(
+                list(Environment.instance(self.path).items()),
+                [('KEY_A', 'value_a'), ('KEY_B', 'value_b')] + list(os.environ.items()),
+            )
+        finally:
+            Environment._instance = None
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to