예제 #1
0
    def clientConnectionMade(self, broker):
        if self.sslContextFactory and not self.sslContextFactory.certificate_verified:
            self.remote_client.log.error(
                "A remote cloud could not prove that its security certificate is "
                "from {host}. This may cause a misconfiguration or an attacker "
                "intercepting your connection.",
                host=self.sslContextFactory.host,
            )
            return self.remote_client.disconnect()
        pb.PBClientFactory.clientConnectionMade(self, broker)
        protocol.ReconnectingClientFactory.resetDelay(self)
        self.remote_client.log.info("Successfully connected")
        self.remote_client.log.info("Authenticating")

        auth_token = None
        try:
            auth_token = AccountClient().fetch_authentication_token()
        except Exception as e:  # pylint:disable=broad-except
            d = defer.Deferred()
            d.addErrback(self.clientAuthorizationFailed)
            d.errback(pb.Error(e))
            return d

        d = self.login(
            credentials.UsernamePassword(
                auth_token.encode(),
                get_host_id().encode(),
            ),
            client=self.remote_client,
        )
        d.addCallback(self.remote_client.cb_client_authorization_made)
        d.addErrback(self.clientAuthorizationFailed)
        return d
예제 #2
0
def team_create(orgname_teamname, description):
    orgname, teamname = orgname_teamname.split(":", 1)
    client = AccountClient()
    client.create_team(orgname, teamname, description)
    return click.secho(
        "The team %s has been successfully created." % teamname, fg="green",
    )
예제 #3
0
def team_list(orgname, json_output):
    client = AccountClient()
    data = {}
    if not orgname:
        for item in client.list_orgs():
            teams = client.list_teams(item.get("orgname"))
            data[item.get("orgname")] = teams
    else:
        teams = client.list_teams(orgname)
        data[orgname] = teams
    if json_output:
        return click.echo(json.dumps(data[orgname] if orgname else data))
    if not any(data.values()):
        return click.secho("You do not have any teams.", fg="yellow")
    for org_name in data:
        for team in data[org_name]:
            click.echo()
            click.secho("%s:%s" % (org_name, team.get("name")), fg="cyan")
            click.echo("-" * len("%s:%s" % (org_name, team.get("name"))))
            table_data = []
            if team.get("description"):
                table_data.append(("Description:", team.get("description")))
            table_data.append((
                "Members:",
                ", ".join(
                    (member.get("username") for member in team.get("members")))
                if team.get("members") else "-",
            ))
            click.echo(tabulate(table_data, tablefmt="plain"))
    return click.echo()
예제 #4
0
 def publish_package(self,
                     archive_path,
                     owner=None,
                     released_at=None,
                     private=False,
                     notify=True):
     account = AccountClient()
     if not owner:
         owner = (account.get_account_info(
             offline=True).get("profile").get("username"))
     with open(archive_path, "rb") as fp:
         return self.send_auth_request(
             "post",
             "/v3/packages/%s/%s" %
             (owner, PackageType.from_archive(archive_path)),
             params={
                 "private": 1 if private else 0,
                 "notify": 1 if notify else 0,
                 "released_at": released_at,
             },
             headers={
                 "Content-Type":
                 "application/octet-stream",
                 "X-PIO-Content-SHA256":
                 fs.calculate_file_hashsum("sha256", archive_path),
             },
             data=fp,
         )
예제 #5
0
def org_create(orgname, email, displayname):
    client = AccountClient()
    client.create_org(orgname, email, displayname)
    return click.secho(
        "The organization `%s` has been successfully created." % orgname,
        fg="green",
    )
예제 #6
0
def account_forgot(username):
    client = AccountClient()
    client.forgot_password(username)
    return click.secho(
        "If this account is registered, we will send the "
        "further instructions to your email.",
        fg="green",
    )
예제 #7
0
def org_remove_owner(orgname, username):
    client = AccountClient()
    client.remove_org_owner(orgname, username)
    return click.secho(
        "The `%s` owner has been successfully removed from the `%s` organization."
        % (username, orgname),
        fg="green",
    )
예제 #8
0
def org_add_owner(orgname, username):
    client = AccountClient()
    client.add_org_owner(orgname, username)
    return click.secho(
        "The new owner `%s` has been successfully added to the `%s` organization."
        % (username, orgname),
        fg="green",
    )
예제 #9
0
def account_register(username, email, password, firstname, lastname):
    client = AccountClient()
    client.registration(username, email, password, firstname, lastname)
    return click.secho(
        "An account has been successfully created. "
        "Please check your mail to activate your account and verify your email address.",
        fg="green",
    )
예제 #10
0
def team_add_member(orgname_teamname, username):
    orgname, teamname = orgname_teamname.split(":", 1)
    client = AccountClient()
    client.add_team_member(orgname, teamname, username)
    return click.secho(
        "The new member %s has been successfully added to the %s team." %
        (username, teamname),
        fg="green",
    )
예제 #11
0
def team_remove_owner(orgname_teamname, username):
    orgname, teamname = orgname_teamname.split(":", 1)
    client = AccountClient()
    client.remove_team_member(orgname, teamname, username)
    return click.secho(
        "The %s member has been successfully removed from the %s team." %
        (username, teamname),
        fg="green",
    )
예제 #12
0
def account_destroy(orgname):
    client = AccountClient()
    click.confirm(
        "Are you sure you want to delete the `%s` organization account?\n"
        "Warning! All linked data will be permanently removed and can not be restored."
        % orgname,
        abort=True,
    )
    client.destroy_org(orgname)
    return click.secho("Organization `%s` has been destroyed." % orgname, fg="green",)
예제 #13
0
def account_token(password, regenerate, json_output):
    client = AccountClient()
    auth_token = client.auth_token(password, regenerate)
    if json_output:
        return click.echo(
            json.dumps({
                "status": "success",
                "result": auth_token
            }))
    return click.secho("Personal Authentication Token: %s" % auth_token,
                       fg="green")
예제 #14
0
def account_show(offline, json_output):
    client = AccountClient()
    info = client.get_account_info(offline)
    if json_output:
        return click.echo(json.dumps(info))
    click.echo()
    if info.get("profile"):
        print_profile(info["profile"])
    if info.get("packages"):
        print_packages(info["packages"])
    if info.get("subscriptions"):
        print_subscriptions(info["subscriptions"])
    return click.echo()
예제 #15
0
def team_destroy(orgname_teamname):
    orgname, teamname = orgname_teamname.split(":", 1)
    click.confirm(
        click.style("Are you sure you want to destroy the %s team?" % teamname,
                    fg="yellow"),
        abort=True,
    )
    client = AccountClient()
    client.destroy_team(orgname, teamname)
    return click.secho(
        "The team %s has been successfully destroyed." % teamname,
        fg="green",
    )
예제 #16
0
 def unpublish_package(  # pylint: disable=redefined-builtin
     self, type, name, owner=None, version=None, undo=False
 ):
     account = AccountClient()
     if not owner:
         owner = (
             account.get_account_info(offline=True).get("profile").get("username")
         )
     path = "/v3/packages/%s/%s/%s" % (owner, type, name)
     if version:
         path += "/" + version
     return self.send_auth_request(
         "delete", path, params={"undo": 1 if undo else 0},
     )
예제 #17
0
 def call_client(method, *args, **kwargs):
     try:
         client = AccountClient()
         return getattr(client, method)(*args, **kwargs)
     except Exception as e:  # pylint: disable=bare-except
         raise jsonrpc.exceptions.JSONRPCDispatchException(
             code=4003, message="PIO Account Call Error", data=str(e))
예제 #18
0
 def send_auth_request(self, *args, **kwargs):
     headers = kwargs.get("headers", {})
     if "Authorization" not in headers:
         token = AccountClient().fetch_authentication_token()
         headers["Authorization"] = "Bearer %s" % token
     kwargs["headers"] = headers
     return self.fetch_json_data(*args, **kwargs)
예제 #19
0
def account_update(current_password, **kwargs):
    client = AccountClient()
    profile = client.get_profile()
    new_profile = profile.copy()
    if not any(kwargs.values()):
        for field in profile:
            new_profile[field] = click.prompt(field.replace("_",
                                                            " ").capitalize(),
                                              default=profile[field])
            if field == "email":
                validate_email(new_profile[field])
            if field == "username":
                validate_username(new_profile[field])
    else:
        new_profile.update(
            {key: value
             for key, value in kwargs.items() if value})
    client.update_profile(new_profile, current_password)
    click.secho("Profile successfully updated!", fg="green")
    username_changed = new_profile["username"] != profile["username"]
    email_changed = new_profile["email"] != profile["email"]
    if not username_changed and not email_changed:
        return None
    try:
        client.logout()
    except AccountNotAuthorized:
        pass
    if email_changed:
        return click.secho(
            "Please check your mail to verify your new email address and re-login. ",
            fg="yellow",
        )
    return click.secho("Please re-login.", fg="yellow")
예제 #20
0
def team_update(orgname_teamname, **kwargs):
    orgname, teamname = orgname_teamname.split(":", 1)
    client = AccountClient()
    team = client.get_team(orgname, teamname)
    del team["id"]
    del team["members"]
    new_team = team.copy()
    if not any(kwargs.values()):
        for field in team:
            new_team[field] = click.prompt(
                field.replace("_", " ").capitalize(), default=team[field]
            )
            if field == "name":
                validate_teamname(new_team[field])
    else:
        new_team.update({key: value for key, value in kwargs.items() if value})
    client.update_team(orgname, teamname, new_team)
    return click.secho(
        "The team %s has been successfully updated." % teamname, fg="green",
    )
예제 #21
0
def org_list(json_output):
    client = AccountClient()
    orgs = client.list_orgs()
    if json_output:
        return click.echo(json.dumps(orgs))
    if not orgs:
        return click.echo("You do not have any organization")
    for org in orgs:
        click.echo()
        click.secho(org.get("orgname"), fg="cyan")
        click.echo("-" * len(org.get("orgname")))
        data = []
        if org.get("displayname"):
            data.append(("Display Name:", org.get("displayname")))
        if org.get("email"):
            data.append(("Email:", org.get("email")))
        data.append((
            "Owners:",
            ", ".join((owner.get("username") for owner in org.get("owners"))),
        ))
        click.echo(tabulate(data, tablefmt="plain"))
    return click.echo()
예제 #22
0
def org_update(cur_orgname, **kwargs):
    client = AccountClient()
    org = client.get_org(cur_orgname)
    del org["owners"]
    new_org = org.copy()
    if not any(kwargs.values()):
        for field in org:
            new_org[field] = click.prompt(
                field.replace("_", " ").capitalize(), default=org[field]
            )
            if field == "email":
                validate_email(new_org[field])
            if field == "orgname":
                validate_orgname(new_org[field])
    else:
        new_org.update(
            {key.replace("new_", ""): value for key, value in kwargs.items() if value}
        )
    client.update_org(cur_orgname, new_org)
    return click.secho(
        "The organization `%s` has been successfully updated." % cur_orgname,
        fg="green",
    )
예제 #23
0
def account_destroy():
    client = AccountClient()
    click.confirm(
        "Are you sure you want to delete the %s user account?\n"
        "Warning! All linked data will be permanently removed and can not be restored."
        % client.get_account_info().get("profile").get("username"),
        abort=True,
    )
    client.destroy_account()
    try:
        client.logout()
    except AccountNotAuthorized:
        pass
    return click.secho(
        "User account has been destroyed.",
        fg="green",
    )
예제 #24
0
def account_logout():
    client = AccountClient()
    client.logout()
    return click.secho("Successfully logged out!", fg="green")
예제 #25
0
def account_password(old_password, new_password):
    client = AccountClient()
    client.change_password(old_password, new_password)
    return click.secho("Password successfully changed!", fg="green")
예제 #26
0
 def clientAuthorizationFailed(self, err):
     AccountClient.delete_local_session()
     self.remote_client.cb_client_authorization_failed(err)
예제 #27
0
def account_login(username, password):
    client = AccountClient()
    client.login(username, password)
    return click.secho("Successfully logged in!", fg="green")