Skip to content

Dynamic Connection Parameters #398

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 26, 2017

Conversation

davidcochrum
Copy link
Contributor

@davidcochrum davidcochrum commented Oct 19, 2016

Sometimes your connection information may need to be dynamic. Dynamic connection parameters allow you to supply or override parameters programmatically through a service.

e.g. In a scenario when the vhost parameter of the connection depends on the current tenant of your white-labeled application and you do not want (or can't) change it's configuration every time.

Define a service under connection_parameters_provider that implements the ConnectionParametersProviderInterface, and add it to the appropriate connections configuration.

connections:
    default:
        host:     'localhost'
        port:     5672
        user:     'guest'
        password: 'guest'
        vhost:    'foo' # to be dynamically overridden by `connection_parameters_provider`
        connection_parameters_provider: connection_parameters_provider_service

Example Implementation:

class ConnectionParametersProviderService implements ConnectionParametersProvider {
    ...
    public function getConnectionParameters() {
        return array('vhost' => $this->getVhost());
    }
    ...
}

In this case, the vhost parameter will be overridden by the output of getVhost().

Sometimes your connection information may need to be dynamic. Dynamic connection parameters allow you to supply or
override parameters programmatically through a service.

e.g. In a scenario when the `vhost` parameter of the connection depends on the current tenant of your white-labeled
application and you do not want (or can't) change it's configuration every time.

Define a service under `connection_parameters_provider` that implements the `ConnectionParametersProviderInterface`,
and add it to the appropriate `connections` configuration.

```yaml
connections:
    default:
        host:     'localhost'
        port:     5672
        user:     'guest'
        password: 'guest'
        vhost:    'foo' # to be dynamically overridden by `connection_parameters_provider`
        connection_parameters_provider: connection_parameters_provider_service
```

Example Implementation:

```php
class ConnectionParametersProviderService implements ConnectionParametersProvider {
    ...
    public function getConnectionParameters() {
        return array('vhost' => $this->getVhost());
    }
    ...
}
```

In this case, the `vhost` parameter will be overridden by the output of `getVhost()`.
@davidcochrum davidcochrum changed the title Dynamic Connection Parameters (#1) Dynamic Connection Parameters Oct 19, 2016
@mvrhov
Copy link
Contributor

mvrhov commented Oct 21, 2016

IMO, there is no need anymore, the symfony 3.2 supports environment variables which are resolved at runtime. So All you would need to set is an environment variable in nginx/apache

@davidcochrum
Copy link
Contributor Author

In my case, I needed the vhost to be dynamically provided by a service. That is not possible via environment variable.

@crussell52
Copy link

crussell52 commented Nov 22, 2016

@mvrhov Thank you for your feedback. Unfortunately, our org is not yet running symfony 3.2, but we'll be looking into the functionality you describe.

However, I do not believe that it obviates this feature. Using an environment variable assumes that the connection configuration is embedded in the URL. In white-label or multi-tenant applications, it is more likely that connection settings need to be loaded from the database based on who is authenticated or what site is being accessed. Any sort of lookup would be outside the scope of the web-server's responsibility.

As a more concrete example, consider dynamic credentials. It seems reasonable to want to roll separate credentials for each vhost to mitigate the risk of a successful user attempt to coerce the active vhost. Trying to deriving credentials via apache/nginx would be an inappropriate solution.

edited for grammar

@crussell52
Copy link

@mvrhov

Thank you for your earlier suggestion of using environment variables with Symfony 3.2... Unfortunately, that did not solve the problem. 😞

The short version is that the configuration is stored in a database so it requires code to extract it. This PR meets that need.

Are there other concerns about this feature?
Are there another way to do this within this library that we've overlooked?

@lombartec
Copy link

Just was looking for this feature in the docs, would be really nice to have.

@crussell52
Copy link

Any progress on this?

This enhancement implementation appears to:

  • Be backwards compatible
  • Have test coverage
  • Solve a valid use case (dynamic connection parameters -- e.g. those stored in a database)
  • Be consistent with existing features (QueuesProviderInterface, QueueOptionsProviderInterface)

Is there anything additional we can contribute to help this merge go through?

@skafandri skafandri merged commit 826a536 into php-amqplib:master May 26, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants