aws / aws-tools-for-powershell

The AWS Tools for PowerShell lets developers and administrators manage their AWS services from the PowerShell scripting environment.
Apache License 2.0
238 stars 79 forks source link

Calling Set-AWSCredential yields exception: "Object reference not set to an instance of an object." #16

Closed pcgeek86 closed 5 years ago

pcgeek86 commented 5 years ago

Expected Behavior

The Set-AWSCredential -ProfileName MyMfaSessionToken command should simply set the "default" AWS credentials profile for the current PowerShell session, according to the documentation.

https://docs.aws.amazon.com/powershell/latest/userguide/specifying-your-aws-credentials.html

Use Set-AWSCredential to specify a default profile for a particular session. This profile overrides any default profile for the duration of the session. This is recommended if you want to use a custom-named profile in your session instead of the current default profile.

Current Behavior

I'm receiving an error message.

Object reference not set to an instance of an object.

Possible Solution

Not sure.

Steps to Reproduce (for bugs)

Set-AWSCredential -ProfileName MyMfaSessionToken

Context

I have my "default" AWS credentials profile configured with my Access Key ID and Secret Key. These credentials give me access to call Get-STSSessionToken, which I use to retrieve an AWS Session Token via Multi-Factor Authentication (MFA).

I'm using the PsIni module to write the AWS Session Token back to my AWS credentials file, under a profile name called MyMfaSessionToken.

Once configured, I want to set this profile as the "default" AWS credentials profile for my PowerShell session. That is when I call Set-AWSCredential and encounter the error message posted above.

Your Environment

Name                           Value
----                           -----
PSVersion                      6.2.0
PSEdition                      Core
GitCommitId                    6.2.0
OS                             Darwin 18.6.0 Darwin Kernel Version 18.6.0: Thu Apr 25 23:16:27 PDT 2019; root:xnu-4903.261.4~2/R…
Platform                       Unix
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

AWS Tools for PowerShell Core
Version 3.3.509.0
Copyright 2012-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.

Amazon Web Services SDK for .NET
Core Runtime Version 3.3.101.9
Copyright 2009-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
matteo-prosperi commented 5 years ago

Could you provide a stack trace for the error? I think a nice way to provide all available detail is to use Resolve-Error as described here https://devblogs.microsoft.com/powershell/resolve-error/

pcgeek86 commented 5 years ago

Sure, let me know how this looks. @matteo-prosperi

PSMessageDetails      :
Exception             : System.NullReferenceException: Object reference not set to an instance of an object.
                           at
                        System.Management.Automation.CmdletParameterBinderController.GetDefaultParameterValuePairsHelper(String
                        cmdletName, String paramName, Object paramValue, IDictionary`2 bindableParameters, IDictionary`2
                        bindableAlias, Dictionary`2 result, HashSet`1 parametersToRemove)
                           at System.Management.Automation.CmdletParameterBinderController.GetDefaultParameterValuePairs(Boolean
                        needToGetAlias)
                           at System.Management.Automation.CmdletParameterBinderController.ApplyDefaultParameterBinding(String
                        bindingStage, Boolean isDynamic)
                           at System.Management.Automation.CmdletParameterBinderController.BindCommandLineParametersNoValidation(
                        Collection`1 arguments)
                           at
                        System.Management.Automation.CmdletParameterBinderController.BindCommandLineParameters(Collection`1
                        arguments)
                           at System.Management.Automation.CommandProcessor.BindCommandLineParameters()
                           at System.Management.Automation.CommandProcessor.Prepare(IDictionary psDefaultParameterValues)
                           at System.Management.Automation.CommandProcessorBase.DoPrepare(IDictionary psDefaultParameterValues)
                           at System.Management.Automation.Internal.PipelineProcessor.Start(Boolean incomingStream)
                           at System.Management.Automation.Internal.PipelineProcessor.SynchronousExecuteEnumerate(Object input)
                           at System.Management.Automation.PipelineOps.InvokePipeline(Object input, Boolean ignoreInput,
                        CommandParameterInternal[][] pipeElements, CommandBaseAst[] pipeElementAsts, CommandRedirection[][]
                        commandRedirections, FunctionContext funcContext)
                           at System.Management.Automation.Interpreter.ActionCallInstruction`6.Run(InterpretedFrame frame)
                           at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame
                        frame)
TargetObject          :
CategoryInfo          : OperationStopped: (:) [], NullReferenceException
FullyQualifiedErrorId : System.NullReferenceException
ErrorDetails          :
InvocationInfo        : System.Management.Automation.InvocationInfo
ScriptStackTrace      : at <ScriptBlock>, <No file>: line 1
PipelineIterationInfo : {}

MyCommand             :
BoundParameters       : {}
UnboundArguments      : {}
ScriptLineNumber      : 1
OffsetInLine          : 1
HistoryId             : -1
ScriptName            :
Line                  : Set-AWSCredential -ProfileName MyProfileName
PositionMessage       : At line:1 char:1
                        + Set-AWSCredential -ProfileName MyProfileName
                        + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
PSScriptRoot          :
PSCommandPath         :
InvocationName        :
PipelineLength        : 0
PipelinePosition      : 0
ExpectingInput        : False
CommandOrigin         : Internal
DisplayScriptPosition :

00000000000000000000000000000000000000000000000000000000000000000000000000000000

Message        : Object reference not set to an instance of an object.
Data           : {System.Management.Automation.Interpreter.InterpretedFrameInfo}
InnerException :
TargetSite     : Void GetDefaultParameterValuePairsHelper(System.String, System.String, System.Object, System.Collections.Generic
                 .IDictionary`2[System.String,System.Management.Automation.MergedCompiledCommandParameter], System.Collections.Ge
                 neric.IDictionary`2[System.String,System.Management.Automation.MergedCompiledCommandParameter], System.Collectio
                 ns.Generic.Dictionary`2[System.Management.Automation.MergedCompiledCommandParameter,System.Object],
                 System.Collections.Generic.HashSet`1[System.Management.Automation.MergedCompiledCommandParameter])
StackTrace     :    at System.Management.Automation.CmdletParameterBinderController.GetDefaultParameterValuePairsHelper(String
                 cmdletName, String paramName, Object paramValue, IDictionary`2 bindableParameters, IDictionary`2 bindableAlias,
                 Dictionary`2 result, HashSet`1 parametersToRemove)
                    at System.Management.Automation.CmdletParameterBinderController.GetDefaultParameterValuePairs(Boolean
                 needToGetAlias)
                    at System.Management.Automation.CmdletParameterBinderController.ApplyDefaultParameterBinding(String
                 bindingStage, Boolean isDynamic)
                    at
                 System.Management.Automation.CmdletParameterBinderController.BindCommandLineParametersNoValidation(Collection`1
                 arguments)
                    at System.Management.Automation.CmdletParameterBinderController.BindCommandLineParameters(Collection`1
                 arguments)
                    at System.Management.Automation.CommandProcessor.BindCommandLineParameters()
                    at System.Management.Automation.CommandProcessor.Prepare(IDictionary psDefaultParameterValues)
                    at System.Management.Automation.CommandProcessorBase.DoPrepare(IDictionary psDefaultParameterValues)
                    at System.Management.Automation.Internal.PipelineProcessor.Start(Boolean incomingStream)
                    at System.Management.Automation.Internal.PipelineProcessor.SynchronousExecuteEnumerate(Object input)
                    at System.Management.Automation.PipelineOps.InvokePipeline(Object input, Boolean ignoreInput,
                 CommandParameterInternal[][] pipeElements, CommandBaseAst[] pipeElementAsts, CommandRedirection[][]
                 commandRedirections, FunctionContext funcContext)
                    at System.Management.Automation.Interpreter.ActionCallInstruction`6.Run(InterpretedFrame frame)
                    at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
HelpLink       :
Source         : System.Management.Automation
HResult        : -2147467261
matteo-prosperi commented 5 years ago

That is unexpected. The error trace shows the error happening in internal PowerShell code (System.Management.Automation). I have also tested this with 3.3.509.0 on Linux and I am not getting this error while trying either an existing or unexisting profile.

Could you trace the command and send me the trace? Trace-Command -Name ParameterBinding -Expression { Set-AWSCredential -ProfileName foo } -PSHost

pcgeek86 commented 5 years ago

It turns out this was completely my fault. I had configured a PowerShell profile script that uses $PSDefaultParameterValues to set -AccessKey, -SecretKey and -SessionToken on all AWS commands. Normally I'm against using this, but for a recent need, I chose to give it a try.

Once I tried the Set-AWSCredential -Profile foo command from a pwsh -NoProfile session, it works just fine. Sorry for the noise.