コード例 #1
0
    def joined(self):
        """Indicate the relation is connected and transmit our credentials."""
        log("Updating extension interface with up-to-date data.")

        # Fish out the current zuul address from any relation we have.
        zuul_address = None
        for rid in relation_ids("zuul"):
            for unit in related_units(rid):
                zuul_address = relation_get(
                    rid=rid, unit=unit, attribute="private-address")
                break

        credentials = Credentials()
        for rid in relation_ids("extension"):
            relation_settings = {
                "admin_username": credentials.username(),
                "admin_password": credentials.password(),
                "jenkins_url": "http://%s:8080" % unit_get("private-address"),
                "jenkins-admin-user": credentials.username(),
                "jenkins-token": credentials.token(),
            }
            relation_set(relation_id=rid, relation_settings=relation_settings)
            if zuul_address:
                relation_set(relation_id=rid, zuul_address=zuul_address)

        self.set_state("{relation_name}.connected")
コード例 #2
0
    def joined(self):
        """Indicate the relation is connected and transmit our credentials."""
        log("Updating extension interface with up-to-date data.")

        # Fish out the current zuul address from any relation we have.
        zuul_address = None
        for rid in relation_ids("zuul"):
            for unit in related_units(rid):
                zuul_address = relation_get(
                    rid=rid, unit=unit, attribute="private-address")
                break

        credentials = Credentials()
        for rid in relation_ids("extension"):
            relation_settings = {
                "admin_username": credentials.username(),
                "admin_password": credentials.password(),
                "jenkins_url": "http://%s:8080" % unit_get("private-address"),
                "jenkins-admin-user": credentials.username(),
                "jenkins-token": credentials.token(),
            }
            relation_set(relation_id=rid, relation_settings=relation_settings)
            if zuul_address:
                relation_set(relation_id=rid, zuul_address=zuul_address)

        self.set_state("{relation_name}.connected")
コード例 #3
0
    def _make_client(self):
        """Build a Jenkins client instance."""
        creds = Credentials()
        user = creds.username()
        token = creds.token()

        if token is None:
            creds.token(
                self._get_token(user, creds.password(),
                                self._packages.jenkins_version()))

        client = jenkins.Jenkins(self.url, user, token)
        try:
            client.get_whoami()
        # Handling token regeneration when the current token is invalid.
        # Then re-raise the exception as expected, so the retry kicks off.
        except jenkins.JenkinsException as e:
            if "401" in str(e):
                creds.token(
                    self._get_token(user, creds.password(),
                                    self._packages.jenkins_version()))
            raise
        return client
コード例 #4
0
    def _make_client(self):
        """Build a Jenkins client instance."""
        creds = Credentials()
        user = creds.username()
        token = creds.token()

        # TODO: also handle regenerated tokens
        if token is None:
            client = jenkins.Jenkins(self.url, user, creds.password())
            token = client.run_script(GET_TOKEN_SCRIPT.format(user)).strip()
            creds.token(token)

        client = jenkins.Jenkins(self.url, user, token)
        client.get_whoami()
        return client
コード例 #5
0
ファイル: api.py プロジェクト: jenkinsci/jenkins-charm
    def _make_client(self):
        """Build a Jenkins client instance."""
        creds = Credentials()
        user = creds.username()
        token = creds.token()

        # TODO: also handle regenerated tokens
        if token is None:
            client = jenkins.Jenkins(URL, user, creds.password())
            token = client.run_script(GET_TOKEN_SCRIPT.format(user)).strip()
            creds.token(token)

        client = jenkins.Jenkins(URL, user, token)
        client.get_whoami()
        return client
コード例 #6
0
ファイル: api.py プロジェクト: mthaddon/jenkins-charm
    def _make_client(self):
        """Build a Jenkins client instance."""
        creds = Credentials()
        user = creds.username()
        token = creds.token()

        # TODO: also handle regenerated tokens
        if token is None:
            client = jenkins.Jenkins(self.url, user, creds.password())
            # If we're using Jenkins >= 2.129 we need to request a new token.
            jenkins_version = self._packages.jenkins_version()
            if LooseVersion(jenkins_version) >= LooseVersion('2.129'):
                token = client.run_script(
                    GET_NEW_TOKEN_SCRIPT.format(user)).strip()
            else:
                token = client.run_script(
                    GET_LEGACY_TOKEN_SCRIPT.format(user)).strip()
            creds.token(token)

        client = jenkins.Jenkins(self.url, user, token)
        client.get_whoami()
        return client
コード例 #7
0
class CredentialsTest(CharmTest):

    def setUp(self):
        super(CredentialsTest, self).setUp()
        self.credentials = Credentials()

    def test_username_initial(self):
        """
        Before the initial configuration is bootstrapped, the username
        is the hard-coded 'admin'.
        """
        self.useFixture(AptInstalledJenkins(self.fakes))
        self.assertEqual("admin", self.credentials.username())

    def test_username(self):
        """
        The username matches then one set in the service configuration.
        """
        self.fakes.juju.config["username"] = "******"
        self.useFixture(JenkinsConfiguredAdmin(self.fakes))
        self.assertEqual("joe", self.credentials.username())

    def test_password_initial(self):
        """
        Before the initial configuration is bootstrapped, the password
        is the one written by jenkins.
        """
        self.useFixture(AptInstalledJenkins(self.fakes))
        self.assertEqual(INITIAL_PASSWORD, self.credentials.password())

    def test_password_from_config(self):
        """
        If set, the password matches the one set in the service configuration.
        """
        self.fakes.juju.config["password"] = "******"
        self.useFixture(JenkinsConfiguredAdmin(self.fakes))
        self.assertEqual("sekret", self.credentials.password())

    def test_password_from_local_state(self):
        """
        If not set, the password is retrieved from the local state.
        """
        self.useFixture(JenkinsConfiguredAdmin(self.fakes))
        self.assertEqual(GENERATED_PASSWORD, self.credentials.password())

    def test_token(self):
        """
        The user's API token is initially None.
        """
        self.useFixture(JenkinsConfiguredAdmin(self.fakes))
        self.assertIsNone(self.credentials.token())

    def test_token_set(self):
        """
        The user's API token can be set, and will be saved in the local
        state.
        """
        self.useFixture(JenkinsConfiguredAdmin(self.fakes))
        self.assertEqual("abc", self.credentials.token("abc"))
        self.assertEqual("abc", hookenv.config()["_api-token"])
        self.assertEqual("abc", self.credentials.token())
        self.assertThat(paths.ADMIN_TOKEN, FileContains("abc"))
        self.assertThat(paths.ADMIN_TOKEN, HasOwnership(0, 0))
        self.assertThat(paths.ADMIN_TOKEN, HasPermissions("0600"))
コード例 #8
0
class CredentialsTest(CharmTest):
    def setUp(self):
        super(CredentialsTest, self).setUp()
        self.credentials = Credentials()

    def test_username_initial(self):
        """
        Before the initial configuration is bootstrapped, the username
        is the hard-coded 'admin'.
        """
        self.useFixture(AptInstalledJenkins(self.fakes))
        self.assertEqual("admin", self.credentials.username())

    def test_username(self):
        """
        The username matches then one set in the service configuration.
        """
        self.fakes.juju.config["username"] = "******"
        self.useFixture(JenkinsConfiguredAdmin(self.fakes))
        self.assertEqual("joe", self.credentials.username())

    def test_password_initial(self):
        """
        Before the initial configuration is bootstrapped, the password
        is the one written by jenkins.
        """
        self.useFixture(AptInstalledJenkins(self.fakes))
        self.assertEqual(INITIAL_PASSWORD, self.credentials.password())

    def test_password_from_config(self):
        """
        If set, the password matches the one set in the service configuration.
        """
        self.fakes.juju.config["password"] = "******"
        self.useFixture(JenkinsConfiguredAdmin(self.fakes))
        self.assertEqual("sekret", self.credentials.password())

    def test_password_from_local_state(self):
        """
        If not set, the password is retrieved from the local state.
        """
        self.useFixture(JenkinsConfiguredAdmin(self.fakes))
        self.assertEqual(GENERATED_PASSWORD, self.credentials.password())

    def test_token(self):
        """
        The user's API token is initially None.
        """
        self.useFixture(JenkinsConfiguredAdmin(self.fakes))
        self.assertIsNone(self.credentials.token())

    def test_token_set(self):
        """
        The user's API token can be set, and will be saved in the local
        state.
        """
        self.useFixture(JenkinsConfiguredAdmin(self.fakes))
        self.assertEqual("abc", self.credentials.token("abc"))
        self.assertEqual("abc", hookenv.config()["_api-token"])
        self.assertEqual("abc", self.credentials.token())
        self.assertThat(paths.ADMIN_TOKEN, FileContains("abc"))
        self.assertThat(paths.ADMIN_TOKEN, HasOwnership(0, 0))
        self.assertThat(paths.ADMIN_TOKEN, HasPermissions("0600"))