Skip to content

Commit c3fcb8c

Browse files
Greg MayMnrGreg
Greg May
authored andcommitted
Container RunAsNonroot Add examples to README Change base image
Signed-off-by: Gregory May <[email protected]>
1 parent 1fbd24e commit c3fcb8c

File tree

4 files changed

+118
-12
lines changed

4 files changed

+118
-12
lines changed

README.md

+105-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,110 @@
1-
# powershell-http-template
2-
OpenFaaS HTTP template for PowerShell
31

4-
## Using this template
2+
OpenFaaS PowerShell HTTP function template
3+
=============================================
4+
5+
This template for building Powershell based functions on [OpenFaaS](https://www.openfaas.com), Docker, Knative and Cloud Run.
6+
7+
With this template you can create a new function and deploy it to a platform like [OpenFaaS](https://www.openfaas.com) for:
8+
9+
* scale-to-zero
10+
* horizontal scale-out
11+
* metrics & logs
12+
* automated health-checks
13+
* sane Kubernetes defaults like running as a non-root user
14+
15+
## Status of the template
16+
17+
This template is experimental and I would like your feedback through GitHub issues or [OpenFaaS Slack](https://docs.openfaas.com/community).
18+
19+
## Get started
20+
21+
You can create or scaffold a new function using the [OpenFaaS CLI](https://github.com/openfaas/faas-cli).
22+
523
```
24+
# USERNAME is your Docker Hub account or private Docker registry
25+
$ export USERNAME=alexellisuk
26+
627
$ faas template pull https://github.com/openfaas-incubator/powershell-http-template
7-
$ faas new --lang powershell-http <fn-name>
28+
$ faas new --lang powershell-http-template <fn-name> --prefix="${USERNAME}"
829
```
930

31+
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.
32+
33+
You can now deploy it to OpenFaaS, Knative, Google Cloud Run or even use `docker run`.
34+
35+
See also: [Deploy OpenFaaS](https://docs.openfaas.com/deployment/)
36+
37+
## Example usage
38+
39+
### Minimal string based example
40+
41+
```
42+
function Handler {
43+
Param(
44+
[Parameter(Mandatory=$true)]
45+
[FunctionContext]$fnContext,
46+
[Parameter(Mandatory=$true)]
47+
[FunctionResponse]$fnResponse
48+
)
49+
50+
$output = "Hello! Your input was: " + $fnContext.Body
51+
52+
$fnResponse.Body = $output
53+
54+
}
55+
```
56+
57+
### Minimal JSON based example
58+
59+
```
60+
function Handler {
61+
Param(
62+
[Parameter(Mandatory=$true)]
63+
[FunctionContext]$fnContext,
64+
[Parameter(Mandatory=$true)]
65+
[FunctionResponse]$fnResponse
66+
)
67+
68+
$json = $fnContext.Body | Out-String | ConvertFrom-Json
69+
70+
$key1 = $json.key1
71+
$key2 = $json.key2
72+
73+
$output = @{
74+
"Your JSON input was" = @{
75+
key1=$key1;
76+
key2=$key2;
77+
}
78+
} | ConvertTo-Json -Compress
79+
80+
$fnResponse.Body = $output
81+
82+
}
83+
```
84+
85+
86+
### Example usage with WinRM based remote PowerShell module, environment variables and secrets.
87+
88+
```
89+
function Handler {
90+
Param(
91+
[Parameter(Mandatory=$true)]
92+
[FunctionContext]$fnContext,
93+
[Parameter(Mandatory=$true)]
94+
[FunctionResponse]$fnResponse
95+
)
96+
97+
$username = $env:USERNAME
98+
$password = Get-Content "/var/openfaas/secrets/password" | ConvertTo-SecureString -AsPlainText -Force
99+
$cred = New-Object System.Management.Automation.PSCredential -ArgumentList ($username, $password)
100+
101+
$sessionoptions = New-PSSessionOption -SkipCACheck -SkipCNCheck
102+
$output = Invoke-Command -ComputerName <winrm-server> -Authentication Negotiate -SessionOption $sessionoptions -Credential $cred -ScriptBlock {
103+
Import-module ActiveDirectory
104+
Get-Domain
105+
}
106+
107+
$fnResponse.Body = $output
108+
109+
}
110+
```

template/powershell-http/Dockerfile

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
FROM openfaas/classic-watchdog:0.18.0 as watchdog
1+
FROM openfaas/of-watchdog:0.7.2 as watchdog
22

3-
FROM microsoft/powershell:ubuntu-xenial as ship
3+
FROM microsoft/powershell:centos-7 as ship
44

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

1010
COPY function function
1111

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

1416
ENV fprocess="pwsh ./server.ps1"
1517
ENV cgi_headers="true"
1618
ENV mode="http"
17-
ENV upstream_url="http://127.0.0.1:8081"
19+
ENV upstream_url="http://127.0.0.1:8082"
1820

1921
EXPOSE 8080
22+
23+
HEALTHCHECK --interval=3s CMD [ -e /tmp/.lock ] || exit 1
24+
2025
CMD ["fwatchdog"]

template/powershell-http/function/handler.ps1

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ function Handler {
44
[FunctionContext]$fnContext,
55
[Parameter(Mandatory=$true)]
66
[FunctionResponse]$fnResponse
7-
)
7+
)
88

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

11-
$fnResponse.Body = $output
11+
$fnResponse.Body = $output
1212

1313
}

template/powershell-http/server.ps1

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ Class FunctionResponse{
1616
}
1717

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

2222
. "./function/handler.ps1"

0 commit comments

Comments
 (0)