Exemplo n.º 1
0
 def setUp(self):
     """Mock of responses generated by Spring Cloud Config."""
     self.config_example = {
         "health": {
             "config": {
                 "enabled": False
             }
         },
         "spring": {
             "cloud": {
                 "consul": {
                     "discovery": {
                         "health-check-interval": "10s",
                         "health-check-path": "/manage/health",
                         "instance-id": "pecas-textos:${random.value}",
                         "prefer-ip-address": True,
                         "register-health-check": True
                     },
                     "host": "discovery",
                     "port": 8500
                 }
             }
         }
     }
     self.obj = ConfigClient(
         app_name='test-app',
         url='{address}/{branch}/{app_name}-{profile}.yaml')
Exemplo n.º 2
0
    def handle(self) -> None:
        filter_options = self.argument("filter") or ""
        host = os.getenv("CONFIGSERVER_ADDRESS", self.option("address"))
        url = os.getenv("CONFIGSERVER_CUSTOM_URL")
        if not url:
            url = f"{host}/{self.option('branch')}/{self.argument('app')}-{self.option('profile')}.json"

        client = ConfigClient(
            address=os.getenv("CONFIGSERVER_ADDRESS", self.option("address")),
            branch=os.getenv("BRANCH", self.option("branch")),
            app_name=os.getenv("APP_NAME", self.argument("app")),
            profile=os.getenv("PROFILE", self.option("profile")),
            url=self.option("url") or url,
            fail_fast=False,
        )

        if self.option("file"):
            self.request_file(client, filter_options)
            raise SystemExit(0)

        content = self.request_config(client, filter_options)
        if self.option("json"):
            self.save_file("output.json", content)
        else:
            self.std_output(filter_options, content)
Exemplo n.º 3
0
 def __init__(self,
              app: web.Application,
              key: str = "config",
              client=ConfigClient()) -> None:
     self._validate_app(app)
     self._validate_client(client)
     client.get_config()
     app[str(key)] = _Config(client.config)
Exemplo n.º 4
0
 def configure_custom_client(self, MockRequests):
     custom_client = ConfigClient(
         address="http://localhost:8888/configuration", app_name="myapp")
     custom_oauth2 = OAuth2(
         access_token_uri="http://localhost/token",
         client_id="id",
         client_secret="secret",
     )
     return CF(oauth2=custom_oauth2, client=custom_client)
Exemplo n.º 5
0
 def test_custom_url_property(self):
     obj = ConfigClient(app_name='test-app',
                        branch='development',
                        url="{address}/{branch}/{profile}-{app_name}.json")
     self.assertIsInstance(obj.url, str)
     self.assertEqual(obj.branch, 'development')
     self.assertEqual(
         obj.url,
         "http://localhost:8888/development/development-test-app.json")
Exemplo n.º 6
0
 def test_custom_properties(self):
     oauth2 = OAuth2(
         access_token_uri='http://localhost/token',
         client_id='id',
         client_secret='secret'
     )
     client = ConfigClient(address='http://localhost', app_name='test_app')
     cf = CF(oauth2=oauth2, client=client)
     self.assertEqual(cf.client, client)
     self.assertEqual(cf.oauth2, oauth2)
Exemplo n.º 7
0
 def test_custom_properties(self, monkeypatch):
     monkeypatch.setattr(requests, "post", ResponseMock)
     oauth2 = OAuth2(
         access_token_uri="http://localhost/token",
         client_id="id",
         client_secret="secret",
     )
     client = ConfigClient(address="http://localhost", app_name="test_app")
     cf = CF(oauth2=oauth2, client=client)
     assert cf.client, client
     assert cf.oauth2, oauth2
Exemplo n.º 8
0
    def __attrs_post_init__(self) -> None:
        if not self.oauth2:
            self.oauth2 = OAuth2(
                access_token_uri=self.cfenv.configserver_access_token_uri(),
                client_id=self.cfenv.configserver_client_id(),
                client_secret=self.cfenv.configserver_client_secret())

        if not self.client:
            self.client = ConfigClient(address=self.cfenv.configserver_uri(),
                                       app_name=self.cfenv.application_name)
        self.oauth2.configure()
Exemplo n.º 9
0
 def configure_custom_client(self, MockRequests):
     custom_client = ConfigClient(
         address='http://localhost:8888/configuration',
         app_name='myapp'
     )
     custom_oauth2 = OAuth2(
         access_token_uri='http://localhost/token',
         client_id='id',
         client_secret='secret'
     )
     return CF(oauth2=custom_oauth2, client=custom_client)
Exemplo n.º 10
0
 def client_with_auth(self, monkeypatch, mocker):
     monkeypatch.setattr(requests, "post", conftest.response_mock_success)
     mocker.patch.object(requests, "get")
     requests.get.return_value = conftest.ResponseMock()
     return ConfigClient(
         app_name="test_app",
         oauth2=OAuth2(
             access_token_uri=
             "https://p-spring-cloud-services.uaa.sys.example.com/oauth/token",
             client_id="p-config-server-example-client-id",
             client_secret="EXAMPLE_SECRET",
         ),
     )
Exemplo n.º 11
0
 def handle(self):
     client = ConfigClient(
         address=os.getenv("CONFIGSERVER_ADDRESS", self.option("address")),
         fail_fast=False,
     )
     try:
         resp = client.encrypt(self.argument("data"),
                               path=self.option("path"))
     except Exception:
         self.line("<options=bold>failed to contact server... </>")
         raise SystemExit(1)
     if not self.option("raw") == "yes":
         self.line(f"'{{cipher}}{resp}'")
     else:
         self.line(resp)
Exemplo n.º 12
0
    def __attrs_post_init__(self) -> None:
        if not self.oauth2:
            self.oauth2 = OAuth2(
                access_token_uri=self.cfenv.configserver_access_token_uri(),
                client_id=self.cfenv.configserver_client_id(),
                client_secret=self.cfenv.configserver_client_secret(),
            )

        if not self.client:
            self.client = ConfigClient(
                address=self.cfenv.configserver_uri(),
                app_name=self.cfenv.application_name,
                profile=self.cfenv.space_name.lower(),
                oauth2=self.oauth2,
            )
Exemplo n.º 13
0
 def handle(self):
     client = ConfigClient(
         address=os.getenv("CONFIGSERVER_ADDRESS", self.option("address")),
         fail_fast=False,
     )
     try:
         data = re.match(r"^.?{cipher}?(?P<name>\w.*)",
                         self.argument("data")).group("name")
     except AttributeError:
         data = self.argument("data")
     try:
         resp = client.decrypt(data, path=self.option("path"))
     except Exception:
         self.line("<options=bold>failed to contact server... </>")
         raise SystemExit(1)
     self.line(resp)
Exemplo n.º 14
0
 def test_integration_with_config_client_inst(self, app, resp_mock):
     FlaskConfig(app, ConfigClient(app_name="myapp"))
     assert isinstance(app.config, _Config)
Exemplo n.º 15
0
 def test_url_setter(self, pattern, expected):
     client = ConfigClient(app_name="test_app")
     client.url = pattern
     assert client.url == expected
Exemplo n.º 16
0
def client_with_auth(monkeypatch, mocker, oauth2):
    monkeypatch.setattr(http, "post", response_mock_success)
    mocker.patch.object(http, "get")
    http.get.return_value = ResponseMock()
    return ConfigClient(app_name="test_app", oauth2=oauth2)
Exemplo n.º 17
0
 def test_fix_url_extension_without_profile(self):
     client = ConfigClient(app_name="simpleweb000",
                           url="{address}/{branch}/{app_name}")
     self.assertEqual(client.url,
                      "http://localhost:8888/master/simpleweb000.json")
Exemplo n.º 18
0
 def inner_method(c=None):
     self.assertEqual(ConfigClient(), c)
Exemplo n.º 19
0
 def test_fail_fast_disabled(self, monkeypatch):
     monkeypatch.setattr(requests, "get", Exception)
     client = ConfigClient(app_name="test_app", fail_fast=False)
     with pytest.raises(ConnectionError):
         client.get_config()
Exemplo n.º 20
0
 def test_custom_url_property(self):
     client = ConfigClient(
         app_name="test_app", url="{address}/{branch}/{profile}-{app_name}.yaml",
     )
     assert client.url == "http://localhost:8888/master/development-test_app.json"
Exemplo n.º 21
0
 def client(self, monkeypatch):
     return ConfigClient(app_name="test_app")
Exemplo n.º 22
0
 def client(self, monkeypatch, scope="module"):
     return ConfigClient(app_name="test_app")
Exemplo n.º 23
0
 def test_integration_with_config_client_inst(self, app, resp_mock):
     AioHttpConfig(app, client=ConfigClient(app_name="myapp"))
     assert isinstance(app["config"], dict)