def __OpenRepo(self, username): is_create = False path = self.__files['repo'].replace('{user}', username) if not (os.path.isfile(path)): # DBテーブル作成 path_sh = os.path.join(self.__path_dir_this, 'repo/create/Create.sh') subprocess.call( shlex.split("bash \"{0}\" \"{1}\"".format(path_sh, path))) self.__repos[username] = dataset.connect('sqlite:///' + path) # DBレコード挿入 authData = web.service.github.api.v3.AuthenticationData.AuthenticationData( ) authData.Load(self.__account, username) # ダミー引数を渡す account = self.__account['Accounts'].find_one(Username=username) twofactor = self.__account['TwoFactors'].find_one( AccountId=account['Id']) authentications = [] if None is not twofactor: authentications.append( TwoFactorAuthentication(account['Username'], account['Password'], twofactor['Secret'])) else: authentications.append( BasicAuthentication(account['Username'], account['Password'])) client = web.service.github.api.v3.Client.Client( self, authentications) inserter = database.src.repo.insert.command.repositories.Inserter.Inserter( self, username, client) inserter.Insert() if not (username in self.__repos.keys()): self.__repos[username] = dataset.connect('sqlite:///' + path)
def Run(self, args): web.log.Log.Log().Logger.debug('Account.Delete') web.log.Log.Log().Logger.debug(args) web.log.Log.Log().Logger.debug('-u: {0}'.format(args.username)) web.log.Log.Log().Logger.debug('--auto: {0}'.format(args.auto)) self.__db = database.Database.Database() self.__db.Initialize() account = self.__db.Accounts['Accounts'].find_one(Username=args.username) web.log.Log.Log().Logger.debug(account) if None is account: web.log.Log.Log().Logger.warning('指定したユーザ {0} がDBに存在しません。削除を中止します。'.format(args.username)) return else: authentications = [] twofactor = self.__db.Accounts['TwoFactors'].find_one(AccountId=account['Id']) if None is not twofactor: authentications.append(TwoFactorAuthentication(account['Username'], account['Password'], twofactor['Secret'])) else: authentications.append(BasicAuthentication(account['Username'], account['Password'])) client = web.service.github.api.v3.Client.Client(self.__db, authentications) # 1. 指定ユーザの全Tokenを削除する(SSHKey設定したTokenのはずなのでSSHKeyも削除される self.__DeleteToken(account, client) # 2. SSHのconfigファイル設定の削除と鍵ファイルの削除 self.__DeleteSshFile(account['Id']) # 3. DB設定値(Account, Repository) self.__DeleteDatabase(account) # * GitHubアカウントの退会はサイトから行うこと # 作成したアカウントのリポジトリDB作成や、作成にTokenが必要なライセンスDBの作成 self.__db.Initialize() return self.__db
def Run(self, args): Log().Logger.debug('Account.Delete') Log().Logger.debug(args) Log().Logger.debug('-u: {0}'.format(args.username)) Log().Logger.debug('--auto: {0}'.format(args.auto)) account = Db().Accounts['Accounts'].find_one(Username=args.username) Log().Logger.debug(account) if None is account: Log().Logger.warning('指定したユーザ {0} がDBに存在しません。削除を中止します。'.format( args.username)) return else: authentications = [] twofactor = Db().Accounts['TwoFactors'].find_one( AccountId=account['Id']) if None is not twofactor: authentications.append( TwoFactorAuthentication(account['Username'], account['Password'], twofactor['Secret'])) else: authentications.append( BasicAuthentication(account['Username'], account['Password'])) client = web.service.github.api.v3.Client.Client(authentications) # 1. 指定ユーザの全Tokenを削除する(SSHKey設定したTokenのはずなのでSSHKeyも削除される self.__DeleteToken(account, client) # 2. SSHのconfigファイル設定の削除と鍵ファイルの削除 self.__DeleteSshFile(account['Id']) # 3. DB設定値(Account, Repository) self.__DeleteDatabase(account)
def __OpenDb(self): # マスターDB生成(ファイル、テーブル、データ挿入) if None is self.__lang: if not os.path.isfile(self.__files['lang']): m = database.src.language.Main.Main(self.__files['lang']) m.Run() self.__lang = dataset.connect('sqlite:///' + self.__files['lang']) if None is self.__api: if not os.path.isfile(self.__files['api']): m = database.src.api.Main.Main(self.__files['api']) m.Run() self.__api = dataset.connect('sqlite:///' + self.__files['api']) if None is self.__gnu_license: if not os.path.isfile(self.__files['gnu_license']): m = database.src.gnu_license.Main.Main( self.__files['gnu_license']) m.Run() self.__gnu_license = dataset.connect('sqlite:///' + self.__files['gnu_license']) # アカウントDB生成(ファイル、テーブル作成。データ挿入はCUIにて行う) if None is self.__account: if not os.path.isfile(self.__files['account']): m = database.src.account.Main.Main(self.__files['account']) m.Create() self.__account = dataset.connect('sqlite:///' + self.__files['account']) # DB作成にTokenが必要なもの if 0 < self.__account['Accounts'].count(): # ライセンスDB生成(ファイル、テーブル作成。データ挿入) if not (os.path.isfile(self.__files['license'])): web.log.Log.Log().Logger.debug(self.__files['license']) account = self.__account['Accounts'].find().next() twofactor = self.__account['TwoFactors'].find_one( AccountId=account['Id']) authentications = [] if None is not twofactor: authentications.append( TwoFactorAuthentication(account['Username'], account['Password'], twofactor['Secret'])) else: authentications.append( BasicAuthentication(account['Username'], account['Password'])) client = web.service.github.api.v3.Client.Client( self, authentications) l = database.src.license.Main.Main(self, client) l.Create() self.__license = dataset.connect('sqlite:///' + self.__files['license']) l.Insert() self.__license = dataset.connect('sqlite:///' + self.__files['license']) # 自分アカウントのリポジトリDB生成(ファイル、テーブル作成。データ挿入) for account in self.__account['Accounts'].find(): self.__OpenRepo(account['Username'])
def __GetClient(self, username=None): if None is username: account = self.__dbs['account']['Accounts'].find().next() else: account = self.__dbs['account']['Accounts'].find_one(Username=username) twofactor = self.__dbs['account']['TwoFactors'].find_one(AccountId=account['Id']) authentications = [] if None is not twofactor: authentications.append(TwoFactorAuthentication(account['Username'], account['Password'], twofactor['Secret'])) else: authentications.append(BasicAuthentication(account['Username'], account['Password'])) return web.service.github.api.v3.Client.Client(self, authentications)
def __GetBasicAuth(self, account): twofactor = Db().Accounts['TwoFactors'].find_one( AccountId=account['Id']) if twofactor is None: return BasicAuthentication(account['Username'], account['Password']) else: return TwoFactorAuthentication(account['Username'], account['Password'], twofactor['Secret'])
def Run(self, args): web.log.Log.Log().Logger.debug('Account.Update') web.log.Log.Log().Logger.debug(args) web.log.Log.Log().Logger.debug('-u: {0}'.format(args.username)) web.log.Log.Log().Logger.debug('-rn: {0}'.format(args.rename)) web.log.Log.Log().Logger.debug('-p: {0}'.format(args.password)) web.log.Log.Log().Logger.debug('-m: {0}'.format(args.mailaddress)) web.log.Log.Log().Logger.debug('-s: {0}'.format(args.ssh_host)) web.log.Log.Log().Logger.debug('-t: {0}'.format( args.two_factor_secret_key)) web.log.Log.Log().Logger.debug('-r: {0}'.format( args.two_factor_recovery_code_file_path)) web.log.Log.Log().Logger.debug('--auto: {0}'.format(args.auto)) self.__db = database.src.Database.Database() self.__db.Initialize() account = self.__db.Accounts['Accounts'].find_one( Username=args.username) web.log.Log.Log().Logger.debug(account) if None is account: web.log.Log.Log().Logger.warning( '指定したユーザ {0} がDBに存在しません。更新を中止します。'.format(args.username)) return authentications = [] if None is not args.two_factor_secret_key: authentications.append( TwoFactorAuthentication(args.username, args.password, args.two_factor_secret_key)) else: authentications.append( BasicAuthentication(args.username, args.password)) client = cui.register.github.api.v3.Client.Client( self.__db, authentications) # Accountsテーブルを更新する(ユーザ名、パスワード、メールアドレス) self.__UpdateAccounts(args, account) # SSH鍵を更新する(APIの削除と新規作成で。ローカルで更新し~/.ssh/configで設定済みとする) self.__UpdateSsh(args, account) # 未実装は以下。 # E. 2FA-Secret # F. 2FA-Recovery-Code # 作成したアカウントのリポジトリDB作成や、作成にTokenが必要なライセンスDBの作成 self.__db.Initialize() return self.__db
def Create(self, username=None, scopes=None): if None is username: username = self.__username authentications = [] account = self.__db.Accounts['Accounts'].find_one(Username=username) if None is account: raise Exception('指定したユーザ {0} はDBに未登録です。登録してから実行してください。'.format(username)) token = self.__GetAccessToken(scopes) if None is not token: authentications.append(OAuthAuthentication(token)) two_factor = self.__db.Accounts['TwoFactors'].find_one(AccountId=account['Id']) if None is not two_factor: authentications.append(TwoFactorAuthentication(account['Username'], account['Password'], two_factor['Secret'])) else: authentications.append(BasicAuthentication(account['Username'], account['Password'])) return authentications
def Run(self, args): web.log.Log.Log().Logger.debug('Account.Insert') web.log.Log.Log().Logger.debug(args) web.log.Log.Log().Logger.debug('-u: {0}'.format(args.username)) web.log.Log.Log().Logger.debug('-p: {0}'.format(args.password)) web.log.Log.Log().Logger.debug('-s: {0}'.format(args.ssh_host)) web.log.Log.Log().Logger.debug('-t: {0}'.format( args.two_factor_secret_key)) web.log.Log.Log().Logger.debug('-r: {0}'.format( args.two_factor_recovery_code_file_path)) web.log.Log.Log().Logger.debug('--auto: {0}'.format(args.auto)) self.__db = database.Database.Database() self.__db.Initialize() account = Db().Accounts.GetAccount(username=args.username) #account = self.__db.Accounts['Accounts'].find_one(Username=args.username) web.log.Log.Log().Logger.debug(account) if None is account: authentications = [] if None is not args.two_factor_secret_key: authentications.append( TwoFactorAuthentication(args.username, args.password, args.two_factor_secret_key)) else: authentications.append( BasicAuthentication(args.username, args.password)) client = web.service.github.api.v3.Client.Client( self.__db, authentications) # 1. Tokenの新規作成 token = client.Authorizations.Create( scopes=['repo', 'delete_repo', 'user', 'admin:public_key'], note='GitHubUserRegister.py {0}'.format( '{0:%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now()))) # 2. APIでメールアドレスを習得する。https://developer.github.com/v3/users/emails/ mailaddress = self.__GetPrimaryMail(client) # 3. SSHの生成と設定 # 起動引数`-s`がないなら if None is args.ssh_host: # 3A-1. SSH鍵の新規作成 ssh_key_gen_params = self.__SshKeyGen(args.username, mailaddress) sshconf = cui.register.SshConfigurator.SshConfigurator() sshconf.Load() host = sshconf.AppendHost( args.username, ssh_key_gen_params['path_file_key_private']) # 3A-2. SSH鍵をGitHubに登録してDBに挿入する j_ssh = client.SshKeys.Create(ssh_key_gen_params['public_key'], title=mailaddress) web.log.Log.Log().Logger.debug(j_ssh) self.__setting = setting.Setting.Setting() if 'SSH' == self.__setting.GitRemote: # 3A-3. SSH接続確認 self.__sshkeygen.CheckSshConnect(host, args.username) else: # ラズパイでなぜかSSH接続確認するとフリーズするので省略。 print('SSH接続確認を省略します。') else: # 3B-1. ~/.ssh/configから指定されたHostデータを取得する sshconf = cui.register.SshConfigurator.SshConfigurator() sshconf.Load() if not (args.ssh_host in sshconf.Hosts.keys()): raise Exception( '存在しないSSH Host名が指定されました。-s引数を指定しなければSSH鍵を新規作成して設定します。既存のSSH鍵を使用するなら~/.ssh/configファイルに設定すると自動で読み取ります。configファイルに設定済みのHost名は次の通りです。 {0}' .format(sshconf.Hosts.keys())) host = args.ssh_host ssh_key_gen_params = self.__LoadSshKeyFile(args, sshconf) # 3B-2.GitHubのSSHにすでに設定されているか確認する j_ssh = self.__GetGitHubSsh(client, args.username, mailaddress, ssh_key_gen_params['public_key']) # 4. 全部成功したらDBにアカウントを登録する Db().Accounts['Accounts'].insert( self.__CreateRecordAccount(args, mailaddress)) #self.__db.Accounts['Accounts'].insert(self.__CreateRecordAccount(args, mailaddress)) #account = self.__db.Accounts['Accounts'].find_one(Username=args.username) account = Db().Accounts['Accounts'].find_one( Username=args.username) web.log.Log.Log().Logger.debug(account) if None is not args.two_factor_secret_key: Db().Accounts['TwoFactors'].insert( self.__CreateRecordTwoFactor(account['Id'], args)) #self.__db.Accounts['TwoFactors'].insert(self.__CreateRecordTwoFactor(account['Id'], args)) """ self.__db.Accounts['AccessTokens'].insert(self.__CreateRecordToken(account['Id'], token, j_ssh['id'])) self.__db.Accounts['SshConfigures'].insert(self.__CreateRecordSshConfigures(account['Id'], host, ssh_key_gen_params)) self.__db.Accounts['SshKeys'].insert(self.__CreateRecordSshKeys(account['Id'], ssh_key_gen_params['private_key'], ssh_key_gen_params['public_key'], j_ssh)) """ Db().Accounts['AccessTokens'].insert( self.__CreateRecordToken(account['Id'], token, j_ssh['id'])) Db().Accounts['SshConfigures'].insert( self.__CreateRecordSshConfigures(account['Id'], host, ssh_key_gen_params)) Db().Accounts['SshKeys'].insert( self.__CreateRecordSshKeys(account['Id'], ssh_key_gen_params['private_key'], ssh_key_gen_params['public_key'], j_ssh)) # 作成したアカウントのリポジトリDB作成や、作成にTokenが必要なライセンスDBの作成 #self.__db.Initialize() return self.__db