public void WhenManagedKeyIsInvalid_ThenParseThrowsArgumentException()
 {
     Assert.Throws <ArgumentException>(() => MetadataAuthorizedKey.Parse(
                                           "login:ssh-rsa key username google-ssh {"));
     Assert.Throws <ArgumentException>(() => MetadataAuthorizedKey.Parse(
                                           "login:ssh-rsa key username google-ssh {}"));
     Assert.Throws <ArgumentException>(() => MetadataAuthorizedKey.Parse(
                                           "login:ssh-rsa key username google-ssh {\"userName\": \"user\", \"expireOn\": null}"));
     Assert.Throws <ArgumentException>(() => MetadataAuthorizedKey.Parse(
                                           "login:ssh-rsa key username google-ssh {\"userName\": \"user\", \"expireOn\": \"x\"}"));
 }
 public void WhenUnmanagedKeyIsInvalid_ThenParseThrowsArgumentException()
 {
     Assert.Throws <ArgumentException>(() => MetadataAuthorizedKey.Parse(
                                           "xxx"));
     Assert.Throws <ArgumentException>(() => MetadataAuthorizedKey.Parse(
                                           "login:ssh-rsa key"));
     Assert.Throws <ArgumentException>(() => MetadataAuthorizedKey.Parse(
                                           "login: key username"));
     Assert.Throws <ArgumentException>(() => MetadataAuthorizedKey.Parse(
                                           "login:ssh-rsa key username morejunk"));
 }
Пример #3
0
        public void WhenAddingNewKey_ThenAddReturnsNewSet()
        {
            var metadata = new Metadata.ItemsData()
            {
                Key   = MetadataAuthorizedKeySet.MetadataKey,
                Value = "alice:ssh-rsa key alice"
            };

            var keySet = MetadataAuthorizedKeySet.FromMetadata(metadata)
                         .Add(MetadataAuthorizedKey.Parse("bob:ssh-rsa key notalice"))
                         .Add(MetadataAuthorizedKey.Parse("bob:ssh-rsa key2 bob"));

            Assert.AreEqual(3, keySet.Keys.Count());
        }
Пример #4
0
        public void WhenAddingDuplicateKey_ThenAddReturnsThis()
        {
            var metadata = new Metadata.ItemsData()
            {
                Key   = MetadataAuthorizedKeySet.MetadataKey,
                Value = "alice:ssh-rsa key alice"
            };

            var keySet = MetadataAuthorizedKeySet.FromMetadata(metadata);

            Assert.AreSame(
                keySet,
                keySet.Add(MetadataAuthorizedKey.Parse("alice:ssh-rsa key notalice")));
        }
        public void WhenKeyIsUnmanaged_ThenParseReturnsUnmanagedKey()
        {
            var line = "login:ssh-rsa key user";
            var key  = MetadataAuthorizedKey.Parse(line);

            Assert.IsInstanceOf <UnmanagedMetadataAuthorizedKey>(key);

            Assert.AreEqual("login", key.LoginUsername);
            Assert.AreEqual("ssh-rsa", key.KeyType);
            Assert.AreEqual("key", key.Key);
            Assert.AreEqual("user", ((UnmanagedMetadataAuthorizedKey)key).Username);

            Assert.AreEqual(line, key.ToString());
        }
        public void WhenKeyIsManaged_ThenParseReturnsManagedKey()
        {
            var line = "login:ssh-rsa key google-ssh {\"userName\":\"[email protected]\",\"expireOn\":\"2021-01-15T15:22:35+0000\"}";
            var key  = MetadataAuthorizedKey.Parse(line);

            Assert.IsInstanceOf <ManagedMetadataAuthorizedKey>(key);

            Assert.AreEqual("login", key.LoginUsername);
            Assert.AreEqual("ssh-rsa", key.KeyType);
            Assert.AreEqual("key", key.Key);
            Assert.AreEqual("*****@*****.**", ((ManagedMetadataAuthorizedKey)key).Metadata.Username);
            Assert.AreEqual(new DateTime(2021, 01, 15, 15, 22, 35, 0, DateTimeKind.Utc),
                            ((ManagedMetadataAuthorizedKey)key).Metadata.ExpireOn.ToUniversalTime());

            Assert.AreEqual(line, key.ToString());
        }
        public void WhenKeyIsManagedEcdsaKey_ThenParseReturnsManagedKey()
        {
            var line = "login:ecdsa-sha2-nistp256 AAAA google-ssh {\"userName\":" +
                       "\"[email protected]\",\"expireOn\":\"2015-11-01T10:43:01+0000\"}";
            var key = MetadataAuthorizedKey.Parse(line);

            Assert.IsInstanceOf <ManagedMetadataAuthorizedKey>(key);

            Assert.AreEqual("login", key.LoginUsername);
            Assert.AreEqual("ecdsa-sha2-nistp256", key.KeyType);
            Assert.AreEqual("AAAA", key.Key);
            Assert.AreEqual("*****@*****.**", ((ManagedMetadataAuthorizedKey)key).Metadata.Username);
            Assert.AreEqual(new DateTime(2015, 11, 1, 10, 43, 1, 0, DateTimeKind.Utc),
                            ((ManagedMetadataAuthorizedKey)key).Metadata.ExpireOn.ToUniversalTime());

            Assert.AreEqual(line, key.ToString());
        }
Пример #8
0
        public void WhenSetContainsEntriesWithEmptyUsername_ThenAddMaintainsEntry()
        {
            var metadata = new Metadata.ItemsData()
            {
                Key   = MetadataAuthorizedKeySet.MetadataKey,
                Value = $"alice:ssh-rsa key alice\n" +
                        $":ssh-rsa phantomkey2 phantom\n" +
                        $":ssh-rsa phantomkey3 google-ssh {{\"userName\":\"[email protected]\",\"expireOn\":\"{DateTime.UtcNow.AddMinutes(1):O}\"}}\n" +
                        $"moe:ssh-rsa key2 google-ssh {{\"userName\":\"[email protected]\",\"expireOn\":\"{DateTime.UtcNow.AddMinutes(1):O}\"}}\n"
            };

            var keySet = MetadataAuthorizedKeySet.FromMetadata(metadata)
                         .RemoveExpiredKeys()
                         .Add(MetadataAuthorizedKey.Parse("bob:ssh-rsa key2 bob"));

            Assert.AreEqual(5, keySet.Keys.Count());
            Assert.AreEqual("", keySet.Keys.First(k => k.Key == "phantomkey2").LoginUsername);
            Assert.AreEqual("", keySet.Keys.First(k => k.Key == "phantomkey3").LoginUsername);
        }