def main(argv=sys.argv[1:]): try: con_str = pynimbusauthz.get_db_connection_string() db_obj = DB(con_str=con_str) (opts, args) = setup_options(argv) user_uu = None if len(args) == 1: user_uu = args[0] if opts.new: user = User(db_obj, user_uu, friendly=opts.friendlyname, create=True) pynimbusauthz.print_msg(opts, 0, "User %s added" % (user.get_id())) else: user = User(db_obj, user_uu) pynimbusauthz.print_msg(opts, 0, "User %s" % (user.get_id())) if opts.alias != None: user_alias = user.get_alias(opts.alias, opts.type) if user_alias == None: user_alias = user.create_alias(opts.alias, opts.type, opts.friendlyname) pynimbusauthz.print_msg( opts, 0, "Creating new alias %s:%s" % (opts.type, opts.alias)) if opts.genkey: data = pynimbusauthz.random_string_gen(42) pynimbusauthz.print_msg(opts, 0, "Key generated %s" % (data)) user_alias.set_data(data) elif opts.setkey != None: data = opts.setkey user_alias.set_data(data) pynimbusauthz.print_msg(opts, 0, "updated the alias key") if opts.remove_alias != None: user_alias = user.get_alias(opts.remove_alias, opts.type) user_alias.remove() if opts.remove: pynimbusauthz.print_msg(opts, 1, "Removing user %s" % (user.get_id())) if opts.force: pynimbusauthz.print_msg(opts, 1, "Removing all references") user.destroy_brutally() else: user.destroy() db_obj.commit() except AuthzException, ae: print ae return ae.get_rc()
def test_basic_alias(self): user = User(self.db) uu = user.get_id() aname = "alias1" self.db.commit() rc = pynimbusauthz.add_user.main(["-a", aname, uu]) self.assertEqual(rc, 0, "CLI should return success") ua = user.get_alias(aname, pynimbusauthz.alias_type_s3) self.assertNotEqual(ua, None, "alias not found") rc = pynimbusauthz.add_user.main(["-x", aname, uu]) self.assertEqual(rc, 0, "CLI should return success") ua = user.get_alias(aname, pynimbusauthz.alias_type_s3) self.assertEqual(ua, None, "alias should not be found")
def test_delete_alias(self): user1 = User(self.db) alias1 = user1.create_alias("/name/", pynimbusauthz.alias_type_s3, "name@") alias1.remove() alias2 = user1.get_alias("/name/", pynimbusauthz.alias_type_s3) self.assertEqual(alias2, None, "The alias should be gone")
def main(argv=sys.argv[1:]): try: con_str = pynimbusauthz.get_db_connection_string() db_obj = DB(con_str=con_str) (opts,args) = setup_options(argv) user_uu = None if len(args) == 1: user_uu = args[0] if opts.new: user = User(db_obj, user_uu, friendly=opts.friendlyname, create=True) pynimbusauthz.print_msg(opts, 0, "User %s added" % (user.get_id())) else: user = User(db_obj, user_uu) pynimbusauthz.print_msg(opts, 0, "User %s" % (user.get_id())) if opts.alias != None: user_alias = user.get_alias(opts.alias, opts.type) if user_alias == None: user_alias = user.create_alias(opts.alias, opts.type, opts.friendlyname) pynimbusauthz.print_msg(opts, 0, "Creating new alias %s:%s" % (opts.type,opts.alias)) if opts.genkey: data = pynimbusauthz.random_string_gen(42) pynimbusauthz.print_msg(opts, 0, "Key generated %s" % (data)) user_alias.set_data(data) elif opts.setkey != None: data = opts.setkey user_alias.set_data(data) pynimbusauthz.print_msg(opts, 0, "updated the alias key") if opts.remove_alias != None: user_alias = user.get_alias(opts.remove_alias, opts.type) user_alias.remove() if opts.remove: pynimbusauthz.print_msg(opts, 1, "Removing user %s" % (user.get_id())) if opts.force: pynimbusauthz.print_msg(opts, 1, "Removing all references") user.destroy_brutally() else: user.destroy() db_obj.commit() except AuthzException, ae: print ae return ae.get_rc()
def test_alias_lookup_simple(self): user1 = User(self.db) alias1 = user1.create_alias("/name/", pynimbusauthz.alias_type_s3, "name1@") alias2 = user1.get_alias("/name/", pynimbusauthz.alias_type_s3) self.assertEqual(alias1.get_name(), alias2.get_name(), "Alias names should be the same") self.assertEqual(alias1.get_type(), alias2.get_type(), "Alias types should be the same") self.assertEqual(alias1.get_data(), alias2.get_data(), "Alias data should be the same")
def test_set_alias_data(self): user1 = User(self.db) self.db.commit() alias_name = "helloname" alias1 = user1.create_alias(alias_name, pynimbusauthz.alias_type_s3, alias_name) key = "helloworld" alias1.set_data(key) self.db.commit() alias2 = user1.get_alias(alias_name, pynimbusauthz.alias_type_s3) self.assertEqual(alias1.get_data(), alias2.get_data(), "alias not equal") self.assertEqual(key, alias2.get_data(), "alias not equal")
def test_user_alias_remove(self): aname = str(uuid.uuid1()) uu = str(uuid.uuid1()) rc = pynimbusauthz.add_user.main(["-n", "-a", aname, uu]) self.assertEqual(rc, 0, "CLI should return success") user = User(self.db, uu) ua = user.get_alias(aname, pynimbusauthz.alias_type_s3) rc = pynimbusauthz.add_user.main(["-x", aname, "-r", uu]) self.assertEqual(rc, 0, "CLI should return success") try: user = User(self.db, uu) self.fail("should have had an exception loading user") except: pass try: ua = user.get_alias(aname, pynimbusauthz.alias_type_s3) self.fail("should have had an exception loading user") except: pass
def test_set_key(self): aname = str(uuid.uuid1()) uu = str(uuid.uuid1()) rc = pynimbusauthz.add_user.main(["-n", "-a", aname, uu]) self.assertEqual(rc, 0, "CLI should return success") rc = pynimbusauthz.add_user.main(["-g", "-a", aname, uu]) self.assertEqual(rc, 0, "CLI should return success") key = str(uuid.uuid1()) rc = pynimbusauthz.add_user.main(["-k", key, "-a", aname, uu]) self.assertEqual(rc, 0, "CLI should return success") user = User(self.db, uu) ua = user.get_alias(aname, pynimbusauthz.alias_type_s3) self.db.commit() k2 = ua.get_data() self.assertEqual(k2, key)