posh-awsvault.psm1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
function New-AWSVaultAlias {
  <#
    .SYNOPSIS
      Creates an aws-vault alias.

    .DESCRIPTION
      When called with a command name, creates an alias that does the equivalent of
      aws-vault exec $Env:AWS_PROFILE command.

    .PARAMETER AliasName
      The name of the alias.

    .PARAMETER CommandName
      The command to execute. If omitted, it will be assumed to be the same as
      AliasName.

    .LINK
      https://www.github.com/jonscheiding/posh-awsvault
  #>


  param(
    [Parameter(Position = 0, Mandatory = $true)]
    [string] $AliasName,
    [Parameter(Position = 1)]
    [string] $CommandName
  )

  $ModuleName = "posh-awsvault-$AliasName"
  if([string]::IsNullOrEmpty($CommandName)) {
    $CommandName = $AliasName
  }

  $Module = New-Module -Name $ModuleName -ArgumentList @($AliasName, $CommandName) {
    param(
      [Parameter(Position = 0)] $AliasName,
      [Parameter(Position = 2)] $CommandName
    )

    $FunctionName = "Invoke-AWSVault_$AliasName"

    $FunctionScriptBlock = {
      Invoke-AWSVault $CommandName @args
    }
  
    Set-Item -Path function:\$FunctionName -Value $FunctionScriptBlock
    Set-Alias -Name $AliasName -Value $FunctionName

    Export-ModuleMember -Alias $AliasName -Function $FunctionName
  }
  
  $Module | Import-Module -Force -Global
}

function Remove-AWSVaultAlias {
  <#
    .SYNOPSIS
      Removes an aws-vault alias that was created by New-AWSVaultAlias.

    .PARAMETER AliasName
      The name of the alias to remove.

    .LINK
      https://www.github.com/jonscheiding/posh-awsvault
  #>


  param(
    [Parameter(Position = 0, Mandatory = $true)]
    [string] $AliasName
  )

  $ErrorActionPreference = "stop"

  $Alias = Get-Alias $AliasName
  $Module = $Alias.Module

  if($null -eq $Module -or !$Module.Name.StartsWith("posh-awsvault-")) {
    Write-Error "The alias '$Alias' is not a posh-awsvault alias."
  }

  Remove-Item "Alias:\$($Alias.Name)"
  Remove-Module $Alias.Module.Name
}

function Invoke-AWSVault {
  <#
    .SYNOPSIS
      Provides a convenient wrapper for aws-vault.

    .DESCRIPTION
      Calling this with a command is basically equivalent to calling
      aws-vault exec $Env:AWS_PROFILE command.
      
      But, see https://github.com/99designs/aws-vault/issues/410 for caveats.

    .PARAMETER CommandName
      The command to execute.
    
    .PARAMETER CommandArguments
      The arguments to pass to the command specified by CommandName.

    .LINK
      https://www.github.com/jonscheiding/posh-awsvault
  #>


  param(
    [Parameter(Mandatory = $true)] $CommandName,
    [Parameter(ValueFromRemainingArguments = $true)] $CommandArguments
  )

  $AWSProfile = (Get-Item Env:\AWS_PROFILE -ErrorAction SilentlyContinue)
  $WasAWSProfileSet = $true

  if($null -eq $AWSProfile) {
    $AWSProfile = "default"
    $WasAWSProfileSet = $false
  } else {
    $AWSProfile = $AWSProfile.Value
  }

  Write-Host -ForegroundColor Cyan `
    "Invoking aws-vault for $CommandName with profile $AWSProfile."

  Write-Debug "$CommandName ($CommandArguments)"

  $Command = Get-Command -CommandType Application $CommandName

  try {
    #
    # AWS_PROFILE needs to be unset while calling aws-vault
    # See https://github.com/99designs/aws-vault/issues/410
    #
    Remove-Item Env:\AWS_PROFILE -ErrorAction SilentlyContinue
    Invoke-External aws-vault exec $AWSProfile `-- $Command @CommandArguments
  } finally {
    if($WasAWSProfileSet) {
      Set-Item Env:\AWS_PROFILE $AWSProfile
    }
  }
}

function Invoke-External {
  param(
    [Parameter(Mandatory = $true)] [string] $Command,
    [Parameter(ValueFromRemainingArguments = $true)] $Arguments
  )

  & $Command @Arguments
}

$MyInvocation.MyCommand.ScriptBlock.Module.OnRemove = { 
  Get-Module `
    | Where-Object { $_.Name.StartsWith("posh-awsvault-") } `
    | Remove-Module
}

New-Alias awsv Invoke-AWSVault