Пример #1
0
 public async Task <object> DeleteServiceAccount(dynamic data)
 {
     return(await Task.Run(() => {
         PlatformRefreshTokenCredential CredentialWithProvidedToken = new PlatformRefreshTokenCredential(data._RefreshToken);
         ServiceAccountServiceClient _serviceAccountServiceClient = ServiceAccountServiceClient.Create(credentials: CredentialWithProvidedToken);
         _serviceAccountServiceClient.DeleteServiceAccount(new DeleteServiceAccountRequest
         {
             Id = data._ServiceAccountID
         });
         return data._ServiceAccountID;
     }));
 }
Пример #2
0
 public async Task <object> GetServiceAccountByName(dynamic data)
 {
     return(await Task.Run(() => {
         PlatformRefreshTokenCredential CredentialWithProvidedToken = new PlatformRefreshTokenCredential(data._RefreshToken);
         ServiceAccountServiceClient _serviceAccountServiceClient = ServiceAccountServiceClient.Create(credentials: CredentialWithProvidedToken);
         var _account = _serviceAccountServiceClient.ListServiceAccounts(new ListServiceAccountsRequest
         {
             ProjectName = data._ProjectName
         }).Where(account => account.Name == data._ServiceAccountName);
         if (_account.Count() > 0)
         {
             return _account.First().Id.ToString();
         }
         else
         {
             return null;
         }
     }));
 }
Пример #3
0
        public static void Main(string[] args)
        {
            if (args.Length < 3)
            {
                Console.Out.WriteLine("Usage: <service account token path> <SpatialOS project name> <service account token lifetime in days>");
                Environment.Exit(1);
            }

            var serviceAccountTokenPath = args[0].Trim('"');
            var projectName             = args[1];
            var tokenLifeTimeDays       = int.Parse(args[2]);

            var perm = new Permission
            {
                Parts = { "prj", projectName, "*" },
                Verbs =
                {
                    Permission.Types.Verb.Read,
                    Permission.Types.Verb.Write
                }
            };

            var perm2 = new Permission
            {
                Parts = { "srv", "bundles" },
                Verbs = { Permission.Types.Verb.Read }
            };

            var resp = ServiceAccountServiceClient.Create().CreateServiceAccount(new CreateServiceAccountRequest
            {
                Name        = "dmService",
                ProjectName = projectName,
                Permissions = { perm, perm2 },
                Lifetime    = Duration.FromTimeSpan(new TimeSpan(tokenLifeTimeDays, 0, 0, 0))
            });

            using (var writer = new StreamWriter(Path.GetFullPath(Path.Combine(serviceAccountTokenPath, "ServiceAccountToken.txt"))))
            {
                writer.WriteLine(resp.Token);
            }
        }
Пример #4
0
        public async Task <object> CreateServiceAccount(dynamic data)
        {
            return(await Task.Run(() => {
                PlatformRefreshTokenCredential CredentialWithProvidedToken = new PlatformRefreshTokenCredential(data._RefreshToken);
                ServiceAccountServiceClient _serviceAccountServiceClient = ServiceAccountServiceClient.Create(credentials: CredentialWithProvidedToken);
                var perm = new Permission
                {
                    Parts =
                    {
                        new RepeatedField <string> {
                            "prj", data._ProjectName, "*"
                        }
                    }
                };
                if (data._WritePermission)
                {
                    perm.Verbs.Add(Permission.Types.Verb.Write);
                }
                if (data._ReadPermission)
                {
                    perm.Verbs.Add(Permission.Types.Verb.Read);
                }
                if (data._GrantPermission)
                {
                    perm.Verbs.Add(Permission.Types.Verb.Grant);
                }

                var _newAccount = _serviceAccountServiceClient.CreateServiceAccount(new CreateServiceAccountRequest
                {
                    Name = data._ServiceAccountName,
                    ProjectName = data._ProjectName,
                    Permissions = { new RepeatedField <Permission> {
                                        perm
                                    } },
                    Lifetime = Duration.FromTimeSpan(TimeSpan.FromSeconds(data._Lifetime))
                });
                return _newAccount.Id;
            }));
        }
Пример #5
0
 public async Task <object> IncreaseServiceAccountLifetime(dynamic data)
 {
     return(await Task.Run(() => {
         PlatformRefreshTokenCredential CredentialWithProvidedToken = new PlatformRefreshTokenCredential(data._RefreshToken);
         ServiceAccountServiceClient _serviceAccountServiceClient = ServiceAccountServiceClient.Create(credentials: CredentialWithProvidedToken);
         var _account = _serviceAccountServiceClient.ListServiceAccounts(new ListServiceAccountsRequest
         {
             ProjectName = data._ProjectName
         }).Where(account => account.Id == data._ServiceAccountID);
         if (_account.Count() > 0)
         {
             _serviceAccountServiceClient.UpdateServiceAccount(new UpdateServiceAccountRequest
             {
                 Id = _account.First().Id,
                 ExpirationTime = DateTime.UtcNow.AddSeconds(data._Lifeitime).ToTimestamp()
             });
             return _account.First().Id.ToString();
         }
         else
         {
             return null;
         }
     }));
 }