Skip to content

Commit

Permalink
Merge pull request #203 from kbrock/context_credentials
Browse files Browse the repository at this point in the history
Pass credentials around with context
  • Loading branch information
agrare committed Jun 13, 2024
2 parents ac91090 + deffd73 commit e967a86
Show file tree
Hide file tree
Showing 10 changed files with 30 additions and 20 deletions.
4 changes: 2 additions & 2 deletions exe/floe
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ credentials =

workflows =
args.each_slice(2).map do |workflow, input|
context = Floe::Workflow::Context.new(opts[:context], :input => input)
Floe::Workflow.load(workflow, context, credentials)
context = Floe::Workflow::Context.new(opts[:context], :input => input, :credentials => credentials)
Floe::Workflow.load(workflow, context)
end

# run
Expand Down
13 changes: 10 additions & 3 deletions lib/floe/workflow.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,21 +85,24 @@ def wait(workflows, timeout: nil, &block)
end
end

attr_reader :context, :credentials, :payload, :states, :states_by_name, :start_at, :name, :comment
attr_reader :context, :payload, :states, :states_by_name, :start_at, :name, :comment

def initialize(payload, context = nil, credentials = {}, name = nil)
def initialize(payload, context = nil, credentials = nil, name = nil)
payload = JSON.parse(payload) if payload.kind_of?(String)
credentials = JSON.parse(credentials) if credentials.kind_of?(String)
context = Context.new(context) unless context.kind_of?(Context)

# backwards compatibility
# caller should really put credentials into context and not pass that variable
context.credentials = credentials if credentials

raise Floe::InvalidWorkflowError, "Missing field \"States\"" if payload["States"].nil?
raise Floe::InvalidWorkflowError, "Missing field \"StartAt\"" if payload["StartAt"].nil?
raise Floe::InvalidWorkflowError, "\"StartAt\" not in the \"States\" field" unless payload["States"].key?(payload["StartAt"])

@name = name
@payload = payload
@context = context
@credentials = credentials || {}
@comment = payload["Comment"]
@start_at = payload["StartAt"]

Expand Down Expand Up @@ -175,6 +178,10 @@ def current_state
@states_by_name[context.state_name]
end

# backwards compatibility. Caller should access directly from context
def credentials
@context.credentials
end
private

def step!
Expand Down
6 changes: 5 additions & 1 deletion lib/floe/workflow/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
module Floe
class Workflow
class Context
attr_accessor :credentials

# @param context [Json|Hash] (default, create another with input and execution params)
# @param input [Hash] (default: {})
def initialize(context = nil, input: nil)
def initialize(context = nil, input: nil, credentials: {})
context = JSON.parse(context) if context.kind_of?(String)

input ||= {}
Expand All @@ -18,6 +20,8 @@ def initialize(context = nil, input: nil)
self["StateHistory"] ||= []
self["StateMachine"] ||= {}
self["Task"] ||= {}

@credentials = credentials || {}
end

def execution
Expand Down
4 changes: 2 additions & 2 deletions lib/floe/workflow/states/input_output_mixin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ def process_output(input, results)

results = result_selector.value(context, results) if @result_selector
if result_path.payload.start_with?("$.Credentials")
credentials = result_path.set(workflow.credentials, results)["Credentials"]
workflow.credentials.merge!(credentials)
credentials = result_path.set(context.credentials, results)["Credentials"]
context.credentials.merge!(credentials)
output = input
else
output = result_path.set(input, results)
Expand Down
2 changes: 1 addition & 1 deletion lib/floe/workflow/states/task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def start(input)
super

input = process_input(input)
runner_context = runner.run_async!(resource, input, credentials&.value({}, workflow.credentials), context)
runner_context = runner.run_async!(resource, input, credentials&.value({}, workflow.context.credentials), context)

context.state["RunnerContext"] = runner_context
end
Expand Down
4 changes: 2 additions & 2 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@

# factory methods

def make_workflow(ctx, payload, creds: {})
Floe::Workflow.new(make_payload(payload), ctx, creds)
def make_workflow(ctx, payload)
Floe::Workflow.new(make_payload(payload), ctx)
end

def make_payload(states)
Expand Down
4 changes: 4 additions & 0 deletions spec/workflow/context_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
expect(ctx.execution["api"]).to eq("http://localhost/")
expect(ctx.state).not_to eq(nil)
end

it "defaults credentials to {}" do
expect(ctx.credentials).to eq({})
end
end

describe "#started?" do
Expand Down
2 changes: 1 addition & 1 deletion spec/workflow/states/pass_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@

it "sets the result in Credentials" do
state.run_nonblock!
expect(workflow.credentials).to include({"user" => "luggage", "password" => "1234"})
expect(ctx.credentials).to include({"user" => "luggage", "password" => "1234"})
expect(ctx.next_state).to eq("SuccessState")
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/workflow/states/task_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@

workflow.run_nonblock

expect(workflow.credentials).to include("token" => "shhh!")
expect(ctx.credentials).to include("token" => "shhh!")
expect(ctx.output).to eq(
"foo" => {"bar" => "baz"},
"bar" => {"baz" => "foo"}
Expand Down
9 changes: 2 additions & 7 deletions spec/workflow_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@
expect(ctx.ended?).to eq(false)
end

it "sets credentials to empty hash if nil passed in" do
workflow = make_workflow(ctx, {"FirstState" => {"Type" => "Succeed"}}, :creds => nil)
expect(workflow.credentials).to eq({})
end

it "raises an exception for missing States" do
payload = {"StartAt" => "Nothing"}

Expand Down Expand Up @@ -366,12 +361,12 @@

describe "#comment" do
it "handles no comment" do
workflow = Floe::Workflow.new({"StartAt" => "First", "States" => {"First" => {"Type" => "Succeed"}}}, {})
workflow = Floe::Workflow.new({"StartAt" => "First", "States" => {"First" => {"Type" => "Succeed"}}})
expect(workflow.comment).to be nil
end

it "handles a comment" do
workflow = Floe::Workflow.new({"StartAt" => "First", "Comment" => "great stuff", "States" => {"First" => {"Type" => "Succeed"}}}, {})
workflow = Floe::Workflow.new({"StartAt" => "First", "Comment" => "great stuff", "States" => {"First" => {"Type" => "Succeed"}}})
expect(workflow.comment).to eq("great stuff")
end
end
Expand Down

0 comments on commit e967a86

Please sign in to comment.