Class: Sxn::Rules::CopyFilesRule

Inherits:
BaseRule
  • Object
show all
Defined in:
lib/sxn/rules/copy_files_rule.rb

Overview

CopyFilesRule handles secure copying and linking of files from the project root to the session directory. It uses the SecureFileCopier from the security layer to ensure safe file operations with proper permission handling and optional encryption.

Configuration format: {

"files" => [
  {
    "source" => "config/master.key",
    "destination" => "config/master.key",  # optional, defaults to source
    "strategy" => "copy",                  # or "symlink"
    "permissions" => "0600",               # optional, uses secure defaults
    "encrypt" => false,                    # optional, default false
    "required" => true                     # optional, default true
  }
]

}

Examples:

Basic usage

rule = CopyFilesRule.new(
  "copy_secrets",
  {
    "files" => [
      { "source" => "config/master.key", "strategy" => "copy" },
      { "source" => ".env", "strategy" => "symlink" }
    ]
  },
  "/path/to/project",
  "/path/to/session"
)
rule.validate
rule.apply

Constant Summary collapse

VALID_STRATEGIES =

Supported file operation strategies

%w[copy symlink].freeze
REQUIRE_ENCRYPTION_PATTERNS =

File patterns that should always be encrypted if copied

[
  /master\.key$/,
  /credentials.*\.key$/,
  /\.env\..*key/,
  /auth.*token/i,
  /secret/i
].freeze

Constants included from BaseRule::States

BaseRule::States::APPLIED, BaseRule::States::APPLYING, BaseRule::States::FAILED, BaseRule::States::PENDING, BaseRule::States::ROLLED_BACK, BaseRule::States::ROLLING_BACK, BaseRule::States::VALIDATED, BaseRule::States::VALIDATING

Instance Attribute Summary

Attributes inherited from BaseRule

#changes, #config, #dependencies, #errors, #name, #project_path, #session_path, #state

Instance Method Summary collapse

Methods inherited from BaseRule

#applied?, #can_execute?, #description, #duration, #failed?, #required?, #rollback, #rollbackable?, #to_h, #type, #validate, #validate_config_hash

Constructor Details

#initialize(arg1 = nil, arg2 = nil, arg3 = nil, arg4 = nil, dependencies: []) ⇒ CopyFilesRule

Initialize the copy files rule



58
59
60
61
# File 'lib/sxn/rules/copy_files_rule.rb', line 58

def initialize(arg1 = nil, arg2 = nil, arg3 = nil, arg4 = nil, dependencies: [])
  super
  @file_copier = Sxn::Security::SecureFileCopier.new(@session_path, logger: logger)
end

Instance Method Details

#applyObject

Apply the file copying operations



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/sxn/rules/copy_files_rule.rb', line 66

def apply
  change_state!(APPLYING)

  begin
    @config["files"].each do |file_config|
      apply_file_operation(file_config)
    end

    change_state!(APPLIED)
    log(:info, "Successfully copied #{@config["files"].size} files")
    true
  rescue StandardError => e
    @errors << e
    change_state!(FAILED)
    raise ApplicationError, "Failed to copy files: #{e.message}"
  end
end