Skip to content
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

Make identity naming more consistent + fix bugs #1140

Merged
merged 10 commits into from
May 14, 2024

Conversation

jdetter
Copy link
Collaborator

@jdetter jdetter commented Apr 23, 2024

Description of Changes

Please describe your change, mention any related tickets, and so on here.

  • There are several places in the CLI where you can set the name of an identity:
    • spacetime identity new --name <name> --no-email
    • spacetime identity set-name <identity> <name>
    • spacetime identity import <identity> <token> --name <name>

In these places we had slightly different logic for validating that an identity name looks okay. I've made the logic in these places more consistent. This will prevent people from accidentally adding identities with the same name or with an invalid name (identity names cannot look like identities).

API and ABI breaking changes

If this is an API or ABI breaking change, please apply the
corresponding GitHub label.

Not breaking

Expected complexity level and risk

How complicated do you think these changes are? Grade on a scale from 1 to 5,
where 1 is a trivial change, and 5 is a deep-reaching and complex change.

This complexity rating applies not only to the complexity apparent in the diff,
but also to its interactions with existing and future code.

If you answered more than a 2, explain what is complex about the PR,
and what other components it interacts with in potentially concerning ways.

0

Testing

Describe any testing you've done, and any testing you'd like your reviewers to do,
so that you're confident that all the changes work as expected!

  • Write a test you've completed here.
  • Create a new identity with the name admin. Then create another identity with no name. Export this new identity. Delete the identity. Now import this identity with the name admin, you will get an error.

@jdetter jdetter requested review from bfops and kurtismullins April 23, 2024 12:36
@bfops bfops added release-any To be landed in any release window CLI only This change only affects the CLI behavior labels Apr 24, 2024
@@ -585,6 +577,7 @@ async fn exec_token(config: Config, args: &ArgMatches) -> Result<(), anyhow::Err
async fn exec_set_name(mut config: Config, args: &ArgMatches) -> Result<(), anyhow::Error> {
let new_name = args.get_one::<String>("name").unwrap();
let identity_or_name = args.get_one::<String>("identity").unwrap();
config.can_set_name(Some(identity_or_name.as_str()))?;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a regression. It essentially renders unnamed identities unusable.

   $ spacetime identity new -s local --no-email | tee output
    IDENTITY     631d3896d10a5fa9db73a95a5a2e20981ab71bc32cb908859e449b5eeb886e9a
    NAME         
    EMAIL        
 
   $ cat output | grep 'IDENTITY' | awk '{print $2;}' | tee identity
   631d3896d10a5fa9db73a95a5a2e20981ab71bc32cb908859e449b5eeb886e9a
 
   $ spacetime identity set-name $(cat identity) new-identity-name
-  Updated identity:
-   IDENTITY  631d3896d10a5fa9db73a95a5a2e20981ab71bc32cb908859e449b5eeb886e9a
-   NAME      new-identity-name
+  Error: An identity name cannot be an identity.
+  [1]

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for catching this Zeke that would have been a disaster 😦

@jdetter jdetter requested a review from bfops April 25, 2024 04:13

if config.identity_configs().iter().any(|ic| ic.identity == identity) {
return Err(anyhow::anyhow!("Identity already exists in config"));
};
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've also added a check here to prevent adding the same identity multiple times

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(factored out config.identity_exists so it's somewhat self-describing)

@@ -149,6 +149,10 @@ pub async fn select_identity_config(
server: Option<&str>,
) -> Result<IdentityConfig, anyhow::Error> {
if let Some(identity_or_name) = identity_or_name {
if identity_or_name.is_empty() {
return Err(anyhow::anyhow!("Identity value cannot be empty."));
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why did we need this check? shouldn't get_identity_config("") fail anyway?

}

Ok(())
}
Copy link
Collaborator

@bfops bfops Apr 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: how would you feel about making this like:

pub fn can_set_name(&self, new_nickname: &str) -> Result<(), anyhow::Error> {

(it seems a bit weird to call can_set_name(None)?)

@@ -111,6 +112,8 @@ pub async fn exec(mut config: Config, args: &ArgMatches) -> Result<(), anyhow::E
let build_debug = args.get_flag("debug");
let wasm_file = args.get_one::<PathBuf>("wasm_file");
let database_host = config.get_host_url(server)?;
// Validate the identity first to provide an early error message
util::select_identity_config(&mut config, identity, server).await?;
Copy link
Collaborator

@bfops bfops Apr 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we just move the later code to here, rather than effectively duplicating this check? like could we put this up here:

    let (auth_header, identity) = get_auth_header(&mut config, anon_identity, identity, server)
        .await?
        .unzip();

@@ -152,7 +152,7 @@ pub async fn select_identity_config(
config
.get_identity_config(identity_or_name)
.cloned()
.ok_or_else(|| anyhow::anyhow!("No such identity credentials for identity: {}", identity_or_name))
.ok_or_else(|| anyhow::anyhow!("No identity credentials for identity \"{}\"", identity_or_name))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(the extra "" make it clearer when it's just the empty string)

@bfops bfops changed the base branch from master to bfops/cli-identity-refactor April 26, 2024 17:49
// easily create a new identity with an email
let (auth_header, identity) = get_auth_header(&mut config, anon_identity, identity, server)
.await?
.unzip();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moving this earlier means that we check identity validity before we go through a (potentially very long) build.

@bfops bfops added the Do not merge Do not merge PRs with this label without coordinating further label Apr 26, 2024
Copy link
Collaborator

@bfops bfops left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM once the base PR merges

Base automatically changed from bfops/cli-identity-refactor to master May 14, 2024 11:39
@jdetter jdetter removed the Do not merge Do not merge PRs with this label without coordinating further label May 14, 2024
@jdetter jdetter force-pushed the jdetter/make-identity-naming-more-consistent branch from ae62610 to ce04e2d Compare May 14, 2024 16:15
@jdetter jdetter enabled auto-merge May 14, 2024 16:22
@jdetter jdetter added this pull request to the merge queue May 14, 2024
Merged via the queue into master with commit f13a143 May 14, 2024
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLI only This change only affects the CLI behavior release-any To be landed in any release window
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants