<commit_before># === Synopsis:
#   RightScale Thunker (rs_thunk) - (c) 2011 RightScale Inc
#
#   Thunker allows you to manage custom SSH logins for non root users. It uses the rightscale
#   user and group RightScale to give privileges for authorization, managing the instance
#   under individual users' profiles. It also downloads profile tarballs containing e.g.
#   bash config.
#
## === Usage
#    rs_thunk --username USERNAME --email EMAIL [--profile DATA] [--force]
#
# === Options:
#      --username,  -u USERNAME     Authorize as RightScale user with USERNAME
#      --email,     -e EMAIL        Create audit entry saying "EMAIL logged in as USERNAME"
#      --profile,   -p DATA         Extra profile data (e.g. a URL to download)
#      --force,     -f              If profile option was specified - rewrite existing files
#
# === Examples:
#   Authorize as 'alice' with email address <EMAIL>:
#     rs_thunk -u alice -e <EMAIL>
#
require 'rubygems'
require 'optparse'
require 'right_agent'
require 'right_agent/scripts/usage'
require 'right_agent/scripts/common_parser'
require 'right_agent/core_payload_types'
require 'right_support/net'

basedir = File.expand_path('../..', __FILE__)
require File.join(basedir, 'lib', 'instance')

module RightScale

  class Thunker
    AUDIT_REQUEST_TIMEOUT = 2 * 60  # synchronous tag requests need a long timeout

    class ThunkError < Exception
      attr_reader :code

      def initialize(msg, code=nil)
        super(msg)
        @code = code || 1
      end
    end

    # Manage individual user SSH logins
    #
    # === Parameters
    # options(Hash):: Hash of options as defined in +parse_args+
    #
    # === Return
    # true:: Always return true
    def run(options)
      @log_sink = StringIO.new
      @log = Logger.new(@log_sink)
      RightScale::Log.force_logger(@log)

      username = options.delete(:username)
      email = options.delete(:email)
      profile = options.delete(:profile)
      force = options.delete(:force)

      fail(1) if missing_argument(username, "USERNAME") || missing_argument(email, "EMAIL")

      #Thunk into user's context
      orig = ENV['SSH2_ORIGINAL_COMMAND'] || ENV['SSH_ORIGINAL_COMMAND']

      if orig =~ /^[A-Za-z0-9_/]+scp -[ft]/
        cmd = "sudo -u #{username} #{orig}"
        access = :scp
      elsif orig =~ %r{^[A-Za-z0-9_/]+sftp-server$}
        cmd = "sudo -u #{username} #{orig}"
        access = :sftp
      elsif orig != nil && !orig.empty?
        cmd = "sudo -u #{username} #{orig}"
        access = :ssh_cmd
      else
        cmd = "sudo -i -u #{username}"
        access = :ssh_shell
      end

      client_ip = ENV['SSH_CLIENT'].split(/\s+/).first if ENV.has_key?('SSH_CLIENT')

      create_audit_entry(email, username, access, orig, client_ip)
      create_profile(username, profile, force) if profile
      Kernel.exec(cmd)
    end

    # Create options hash from command line arguments
    #
    # === Return
    # options(Hash):: Hash of options as defined by the command line
    def parse_args<commit_msg>Fix syntax error<commit_after># === Synopsis:
#   RightScale Thunker (rs_thunk) - (c) 2011 RightScale Inc
#
#   Thunker allows you to manage custom SSH logins for non root users. It uses the rightscale
#   user and group RightScale to give privileges for authorization, managing the instance
#   under individual users' profiles. It also downloads profile tarballs containing e.g.
#   bash config.
#
## === Usage
#    rs_thunk --username USERNAME --email EMAIL [--profile DATA] [--force]
#
# === Options:
#      --username,  -u USERNAME     Authorize as RightScale user with USERNAME
#      --email,     -e EMAIL        Create audit entry saying "EMAIL logged in as USERNAME"
#      --profile,   -p DATA         Extra profile data (e.g. a URL to download)
#      --force,     -f              If profile option was specified - rewrite existing files
#
# === Examples:
#   Authorize as 'alice' with email address <EMAIL>:
#     rs_thunk -u alice -e <EMAIL>
#
require 'rubygems'
require 'optparse'
require 'right_agent'
require 'right_agent/scripts/usage'
require 'right_agent/scripts/common_parser'
require 'right_agent/core_payload_types'
require 'right_support/net'

basedir = File.expand_path('../..', __FILE__)
require File.join(basedir, 'lib', 'instance')

module RightScale

  class Thunker
    AUDIT_REQUEST_TIMEOUT = 2 * 60  # synchronous tag requests need a long timeout

    class ThunkError < Exception
      attr_reader :code

      def initialize(msg, code=nil)
        super(msg)
        @code = code || 1
      end
    end

    # Manage individual user SSH logins
    #
    # === Parameters
    # options(Hash):: Hash of options as defined in +parse_args+
    #
    # === Return
    # true:: Always return true
    def run(options)
      @log_sink = StringIO.new
      @log = Logger.new(@log_sink)
      RightScale::Log.force_logger(@log)

      username = options.delete(:username)
      email = options.delete(:email)
      profile = options.delete(:profile)
      force = options.delete(:force)

      fail(1) if missing_argument(username, "USERNAME") || missing_argument(email, "EMAIL")

      #Thunk into user's context
      orig = ENV['SSH2_ORIGINAL_COMMAND'] || ENV['SSH_ORIGINAL_COMMAND']

      if orig =~ %r{^[A-Za-z0-9_/]+scp -[ft]}
        cmd = "sudo -u #{username} #{orig}"
        access = :scp
      elsif orig =~ %r{^[A-Za-z0-9_/]+sftp-server$}
        cmd = "sudo -u #{username} #{orig}"
        access = :sftp
      elsif orig != nil && !orig.empty?
        cmd = "sudo -u #{username} #{orig}"
        access = :ssh_cmd
      else
        cmd = "sudo -i -u #{username}"
        access = :ssh_shell
      end

      client_ip = ENV['SSH_CLIENT'].split(/\s+/).first if ENV.has_key?('SSH_CLIENT')

      create_audit_entry(email, username, access, orig, client_ip)
      create_profile(username, profile, force) if profile
      Kernel.exec(cmd)
    end

    # Create options hash from command line arguments
    #
    # === Return
    # options(Hash):: Hash of options as defined by the command line
    def parse_args
{'fraction_non_alphanumeric': 0.11931724930247825, 'fraction_numerical': 0.0045954373871656, 'mean_word_length': 2.759407772979642, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1956007', 'n_tokens_mistral': 1973, 'n_tokens_neox': 1896, 'n_words': 577}