Skip to content

Commit eb7a60a

Browse files
authored
Merge pull request #9130 from ruslanlazin/issue-9117
9117: Prevent api_key sharing
2 parents b5150ef + fe8c08b commit eb7a60a

File tree

6 files changed

+94
-78
lines changed

6 files changed

+94
-78
lines changed

modules/swagger-codegen/src/main/resources/python/configuration.mustache

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,29 +14,22 @@ import six
1414
from six.moves import http_client as httplib
1515

1616

17-
class TypeWithDefault(type):
18-
def __init__(cls, name, bases, dct):
19-
super(TypeWithDefault, cls).__init__(name, bases, dct)
20-
cls._default = None
21-
22-
def __call__(cls):
23-
if cls._default is None:
24-
cls._default = type.__call__(cls)
25-
return copy.copy(cls._default)
26-
27-
def set_default(cls, default):
28-
cls._default = copy.copy(default)
29-
30-
31-
class Configuration(six.with_metaclass(TypeWithDefault, object)):
17+
class Configuration(object):
3218
"""NOTE: This class is auto generated by the swagger code generator program.
3319

3420
Ref: https://github.com/swagger-api/swagger-codegen
3521
Do not edit the class manually.
3622
"""
3723

24+
_default = None
25+
3826
def __init__(self):
3927
"""Constructor"""
28+
if self._default:
29+
for key in self._default.__dict__.keys():
30+
self.__dict__[key] = copy.copy(self._default.__dict__[key])
31+
return
32+
4033
# Default Base url
4134
self.host = "{{{basePath}}}"
4235
# Temp file folder for downloading files
@@ -95,6 +88,10 @@ class Configuration(six.with_metaclass(TypeWithDefault, object)):
9588
# Safe chars for path_param
9689
self.safe_chars_for_path_param = ''
9790

91+
@classmethod
92+
def set_default(cls, default):
93+
cls._default = default
94+
9895
@property
9996
def logger_file(self):
10097
"""The logger file.

samples/client/petstore-security-test/python/petstore_api/configuration.py

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,29 +23,22 @@
2323
from six.moves import http_client as httplib
2424

2525

26-
class TypeWithDefault(type):
27-
def __init__(cls, name, bases, dct):
28-
super(TypeWithDefault, cls).__init__(name, bases, dct)
29-
cls._default = None
30-
31-
def __call__(cls):
32-
if cls._default is None:
33-
cls._default = type.__call__(cls)
34-
return copy.copy(cls._default)
35-
36-
def set_default(cls, default):
37-
cls._default = copy.copy(default)
38-
39-
40-
class Configuration(six.with_metaclass(TypeWithDefault, object)):
26+
class Configuration(object):
4127
"""NOTE: This class is auto generated by the swagger code generator program.
4228
4329
Ref: https://github.com/swagger-api/swagger-codegen
4430
Do not edit the class manually.
4531
"""
4632

33+
_default = None
34+
4735
def __init__(self):
4836
"""Constructor"""
37+
if self._default:
38+
for key in self._default.__dict__.keys():
39+
self.__dict__[key] = copy.copy(self._default.__dict__[key])
40+
return
41+
4942
# Default Base url
5043
self.host = "https://petstore.swagger.io */ ' \" =end -- \\r\\n \\n \\r/v2 */ ' \" =end -- \\r\\n \\n \\r"
5144
# Temp file folder for downloading files
@@ -104,6 +97,10 @@ def __init__(self):
10497
# Safe chars for path_param
10598
self.safe_chars_for_path_param = ''
10699

100+
@classmethod
101+
def set_default(cls, default):
102+
cls._default = default
103+
107104
@property
108105
def logger_file(self):
109106
"""The logger file.

samples/client/petstore/python-asyncio/petstore_api/configuration.py

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,29 +23,22 @@
2323
from six.moves import http_client as httplib
2424

2525

26-
class TypeWithDefault(type):
27-
def __init__(cls, name, bases, dct):
28-
super(TypeWithDefault, cls).__init__(name, bases, dct)
29-
cls._default = None
30-
31-
def __call__(cls):
32-
if cls._default is None:
33-
cls._default = type.__call__(cls)
34-
return copy.copy(cls._default)
35-
36-
def set_default(cls, default):
37-
cls._default = copy.copy(default)
38-
39-
40-
class Configuration(six.with_metaclass(TypeWithDefault, object)):
26+
class Configuration(object):
4127
"""NOTE: This class is auto generated by the swagger code generator program.
4228
4329
Ref: https://github.com/swagger-api/swagger-codegen
4430
Do not edit the class manually.
4531
"""
4632

33+
_default = None
34+
4735
def __init__(self):
4836
"""Constructor"""
37+
if self._default:
38+
for key in self._default.__dict__.keys():
39+
self.__dict__[key] = copy.copy(self._default.__dict__[key])
40+
return
41+
4942
# Default Base url
5043
self.host = "http://petstore.swagger.io:80/v2"
5144
# Temp file folder for downloading files
@@ -104,6 +97,10 @@ def __init__(self):
10497
# Safe chars for path_param
10598
self.safe_chars_for_path_param = ''
10699

100+
@classmethod
101+
def set_default(cls, default):
102+
cls._default = default
103+
107104
@property
108105
def logger_file(self):
109106
"""The logger file.

samples/client/petstore/python-tornado/petstore_api/configuration.py

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,29 +23,22 @@
2323
from six.moves import http_client as httplib
2424

2525

26-
class TypeWithDefault(type):
27-
def __init__(cls, name, bases, dct):
28-
super(TypeWithDefault, cls).__init__(name, bases, dct)
29-
cls._default = None
30-
31-
def __call__(cls):
32-
if cls._default is None:
33-
cls._default = type.__call__(cls)
34-
return copy.copy(cls._default)
35-
36-
def set_default(cls, default):
37-
cls._default = copy.copy(default)
38-
39-
40-
class Configuration(six.with_metaclass(TypeWithDefault, object)):
26+
class Configuration(object):
4127
"""NOTE: This class is auto generated by the swagger code generator program.
4228
4329
Ref: https://github.com/swagger-api/swagger-codegen
4430
Do not edit the class manually.
4531
"""
4632

33+
_default = None
34+
4735
def __init__(self):
4836
"""Constructor"""
37+
if self._default:
38+
for key in self._default.__dict__.keys():
39+
self.__dict__[key] = copy.copy(self._default.__dict__[key])
40+
return
41+
4942
# Default Base url
5043
self.host = "http://petstore.swagger.io:80/v2"
5144
# Temp file folder for downloading files
@@ -104,6 +97,10 @@ def __init__(self):
10497
# Safe chars for path_param
10598
self.safe_chars_for_path_param = ''
10699

100+
@classmethod
101+
def set_default(cls, default):
102+
cls._default = default
103+
107104
@property
108105
def logger_file(self):
109106
"""The logger file.

samples/client/petstore/python/petstore_api/configuration.py

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,29 +23,22 @@
2323
from six.moves import http_client as httplib
2424

2525

26-
class TypeWithDefault(type):
27-
def __init__(cls, name, bases, dct):
28-
super(TypeWithDefault, cls).__init__(name, bases, dct)
29-
cls._default = None
30-
31-
def __call__(cls):
32-
if cls._default is None:
33-
cls._default = type.__call__(cls)
34-
return copy.copy(cls._default)
35-
36-
def set_default(cls, default):
37-
cls._default = copy.copy(default)
38-
39-
40-
class Configuration(six.with_metaclass(TypeWithDefault, object)):
26+
class Configuration(object):
4127
"""NOTE: This class is auto generated by the swagger code generator program.
4228
4329
Ref: https://github.com/swagger-api/swagger-codegen
4430
Do not edit the class manually.
4531
"""
4632

33+
_default = None
34+
4735
def __init__(self):
4836
"""Constructor"""
37+
if self._default:
38+
for key in self._default.__dict__.keys():
39+
self.__dict__[key] = copy.copy(self._default.__dict__[key])
40+
return
41+
4942
# Default Base url
5043
self.host = "http://petstore.swagger.io:80/v2"
5144
# Temp file folder for downloading files
@@ -104,6 +97,10 @@ def __init__(self):
10497
# Safe chars for path_param
10598
self.safe_chars_for_path_param = ''
10699

100+
@classmethod
101+
def set_default(cls, default):
102+
cls._default = default
103+
107104
@property
108105
def logger_file(self):
109106
"""The logger file.

samples/client/petstore/python/tests/test_pet_api.py

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ def setUpFiles(self):
8080
self.test_file_dir = os.path.realpath(self.test_file_dir)
8181
self.foo = os.path.join(self.test_file_dir, "foo.png")
8282

83+
def tearDown(self):
84+
Configuration.set_default(None)
85+
8386
def test_preload_content_flag(self):
8487
self.pet_api.add_pet(body=self.pet)
8588

@@ -132,15 +135,43 @@ def test_separate_default_client_instances(self):
132135

133136
self.assertNotEqual(pet_api.api_client.user_agent, pet_api2.api_client.user_agent)
134137

135-
def test_separate_default_config_instances(self):
138+
def test_default_config(self):
139+
default = Configuration()
140+
default.host = 'default_host'
141+
default.api_key['api_key'] = 'default_key'
142+
default.api_key_prefix['prefix'] = 'default_prefix'
143+
Configuration.set_default(default)
144+
145+
configuration = Configuration()
146+
self.assertIsNot(configuration, default)
147+
self.assertEqual(configuration.host, default.host)
148+
self.assertEqual(configuration.api_key['api_key'], default.api_key['api_key'])
149+
self.assertEqual(configuration.api_key_prefix['prefix'], default.api_key_prefix['prefix'])
150+
151+
configuration.host = 'some_host'
152+
configuration.api_key['api_key'] = 'some_key'
153+
configuration.api_key_prefix['prefix'] = 'some_prefix'
154+
self.assertEqual(default.host, 'default_host')
155+
self.assertEqual(default.api_key['api_key'], 'default_key')
156+
self.assertEqual(default.api_key_prefix['prefix'], 'default_prefix')
157+
158+
def test_separate_config_instances(self):
136159
pet_api = petstore_api.PetApi()
137160
pet_api2 = petstore_api.PetApi()
138161
self.assertNotEqual(pet_api.api_client.configuration, pet_api2.api_client.configuration)
139162

140-
pet_api.api_client.configuration.host = 'somehost'
141-
pet_api2.api_client.configuration.host = 'someotherhost'
163+
pet_api.api_client.configuration.host = 'some_host'
164+
pet_api2.api_client.configuration.host = 'some_other_host'
142165
self.assertNotEqual(pet_api.api_client.configuration.host, pet_api2.api_client.configuration.host)
143166

167+
pet_api.api_client.configuration.api_key['api_key'] = 'some_key'
168+
pet_api2.api_client.configuration.api_key['api_key'] = 'some_other_key'
169+
self.assertNotEqual(pet_api.api_client.configuration.api_key['api_key'], pet_api2.api_client.configuration.api_key['api_key'])
170+
171+
pet_api.api_client.configuration.api_key_prefix['prefix'] = 'some_prefix'
172+
pet_api2.api_client.configuration.api_key_prefix['prefix'] = 'some_other_prefix'
173+
self.assertNotEqual(pet_api.api_client.configuration.api_key_prefix['prefix'], pet_api2.api_client.configuration.api_key_prefix['prefix'])
174+
144175
def test_async_request(self):
145176
thread = self.pet_api.add_pet(body=self.pet, async_req=True)
146177
response = thread.get()

0 commit comments

Comments
 (0)