Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update documentation, runas nonroot, enable WinRM negotiate authentication #8

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 105 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,110 @@
# powershell-http-template
OpenFaaS HTTP template for PowerShell

## Using this template
OpenFaaS PowerShell HTTP function template
=============================================

This template for building Powershell based functions on [OpenFaaS](https://www.openfaas.com), Docker, Knative and Cloud Run.

With this template you can create a new function and deploy it to a platform like [OpenFaaS](https://www.openfaas.com) for:

* scale-to-zero
* horizontal scale-out
* metrics & logs
* automated health-checks
* sane Kubernetes defaults like running as a non-root user

## Status of the template

This template is experimental and I would like your feedback through GitHub issues or [OpenFaaS Slack](https://docs.openfaas.com/community).

## Get started

You can create or scaffold a new function using the [OpenFaaS CLI](https://github.com/openfaas/faas-cli).

```
# USERNAME is your Docker Hub account or private Docker registry
$ export USERNAME=alexellisuk

$ faas template pull https://github.com/openfaas-incubator/powershell-http-template
$ faas new --lang powershell-http <fn-name>
$ faas new --lang powershell-http <fn-name> --prefix="${USERNAME}"
```

Once you've written your code you can run `faas-cli build` to create a local Docker image, then `faas-cli push` to transfer it to your registry.

You can now deploy it to OpenFaaS, Knative, Google Cloud Run or even use `docker run`.

See also: [Deploy OpenFaaS](https://docs.openfaas.com/deployment/)

## Example usage

### Minimal string based example

```
function Handler {
Param(
[Parameter(Mandatory=$true)]
[FunctionContext]$fnContext,
[Parameter(Mandatory=$true)]
[FunctionResponse]$fnResponse
)

$output = "Hello! Your input was: " + $fnContext.Body

$fnResponse.Body = $output

}
```

### Minimal JSON based example

```
function Handler {
Param(
[Parameter(Mandatory=$true)]
[FunctionContext]$fnContext,
[Parameter(Mandatory=$true)]
[FunctionResponse]$fnResponse
)

$json = $fnContext.Body | Out-String | ConvertFrom-Json

$key1 = $json.key1
$key2 = $json.key2

$output = @{
"Your JSON input was" = @{
key1=$key1;
key2=$key2;
}
} | ConvertTo-Json -Compress

$fnResponse.Body = $output

}
```


### Example usage with WinRM based remote PowerShell module, environment variables and secrets.

```
function Handler {
Param(
[Parameter(Mandatory=$true)]
[FunctionContext]$fnContext,
[Parameter(Mandatory=$true)]
[FunctionResponse]$fnResponse
)

$username = $env:USERNAME
$password = Get-Content "/var/openfaas/secrets/password" | ConvertTo-SecureString -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential -ArgumentList ($username, $password)

$sessionoptions = New-PSSessionOption -SkipCACheck -SkipCNCheck
$output = Invoke-Command -ComputerName <winrm-server> -Authentication Negotiate -SessionOption $sessionoptions -Credential $cred -ScriptBlock {
Import-module ActiveDirectory
Get-Domain
}

$fnResponse.Body = $output

}
```
13 changes: 9 additions & 4 deletions template/powershell-http/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
FROM openfaas/classic-watchdog:0.18.0 as watchdog
FROM openfaas/of-watchdog:0.7.2 as watchdog

FROM microsoft/powershell:ubuntu-xenial as ship
FROM mcr.microsoft.com/powershell:centos-7 as ship

COPY --from=watchdog /fwatchdog /usr/bin/fwatchdog
RUN chmod +x /usr/bin/fwatchdog
Expand All @@ -9,12 +9,17 @@ COPY server.ps1 server.ps1

COPY function function

HEALTHCHECK --interval=1s CMD [ -e /tmp/.lock ] || exit 1
RUN adduser --system --no-create-home --uid 10001 app

USER 10001

ENV fprocess="pwsh ./server.ps1"
ENV cgi_headers="true"
ENV mode="http"
ENV upstream_url="http://127.0.0.1:8081"
ENV upstream_url="http://127.0.0.1:8082"

EXPOSE 8080

HEALTHCHECK --interval=3s CMD [ -e /tmp/.lock ] || exit 1

CMD ["fwatchdog"]
6 changes: 3 additions & 3 deletions template/powershell-http/function/handler.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ function Handler {
[FunctionContext]$fnContext,
[Parameter(Mandatory=$true)]
[FunctionResponse]$fnResponse
)
)

$output = "Hello! Your input was: " + $fnContext.Body
$output = "Hello! Your input was: " + $fnContext.Body

$fnResponse.Body = $output
$fnResponse.Body = $output

}
2 changes: 1 addition & 1 deletion template/powershell-http/server.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Class FunctionResponse{
}

$listener = New-Object System.Net.HttpListener
$listener.Prefixes.Add("http://*:8081/")
$listener.Prefixes.Add("http://*:8082/")
$listener.Start()

. "./function/handler.ps1"
Expand Down