aboutsummaryrefslogtreecommitdiff
path: root/tests/zfs-tests/tests/functional/cli_root/zfs_load-key/zfs_load-key_common.kshlib
blob: f174eeeeaae99aa7eddc211cc4a746b4b499b606 (plain) (blame)
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
158
159
160
161
162
163
164
165
166
#
# CDDL HEADER START
#
# This file and its contents are supplied under the terms of the
# Common Development and Distribution License ("CDDL"), version 1.0.
# You may only use this file in accordance with the terms of version
# 1.0 of the CDDL.
#
# A full copy of the text of the CDDL should have accompanied this
# source.  A copy of the CDDL is also available via the Internet at
# http://www.illumos.org/license/CDDL.
#
# CDDL HEADER END
#

#
# Copyright (c) 2017 Datto, Inc. All rights reserved.
#

. $STF_SUITE/include/libtest.shlib
. $STF_SUITE/tests/functional/cli_root/zfs_load-key/zfs_load-key.cfg

# Return 0 is a dataset key is available, 1 otherwise
#
# $1 - dataset
#
function key_available
{
	typeset ds=$1

	datasetexists $ds || return 1

	typeset val=$(get_prop keystatus $ds)
	if [[ "$val" == "none" ]]; then
		log_note "Dataset $ds is not encrypted"
	elif [[ "$val" == "available" ]]; then
		return 0
	fi

	return 1
}

function key_unavailable
{
	! key_available $1
}

function verify_keyformat
{
	typeset ds=$1
	typeset format=$2
	typeset fmt=$(get_prop keyformat $ds)

	if [[ "$fmt" != "$format" ]]; then
		log_fail "Expected keyformat $format, got $fmt"
	fi

	return 0
}

function verify_keylocation
{
	typeset ds=$1
	typeset location=$2
	typeset keyloc=$(get_prop keylocation $ds)

	if [[ "$keyloc" != "$location" ]]; then
		log_fail "Expected keylocation $location, got $keyloc"
	fi

	return 0
}

function verify_encryption_root
{
	typeset ds=$1
	typeset val=$2
	typeset eroot=$(get_prop encryptionroot $ds)

	if [[ "$eroot" != "$val" ]]; then
		log_note "Expected encryption root '$val', got '$eroot'"
		return 1
	fi

	return 0
}

function verify_origin
{
	typeset ds=$1
	typeset val=$2
	typeset orig=$(get_prop origin $ds)

	if [[ "$orig" != "$val" ]]; then
		log_note "Expected origin '$val', got '$orig'"
		return 1
	fi

	return 0
}

function setup_https
{
	log_must openssl req -x509 -newkey rsa:4096 -sha256 -days 1 -nodes -keyout "/$TESTPOOL/snakeoil.key" -out "$SSL_CA_CERT_FILE" -subj "/CN=$HTTPS_HOSTNAME"

	python3 -uc "
import http.server, ssl, sys, os, time, random

sys.stdin.close()

httpd, err, port = None, None, None
for i in range(1, 100):
	port = random.randint(0xC000, 0xFFFF) # ephemeral range
	try:
		httpd = http.server.HTTPServer(('$HTTPS_HOSTNAME', port), http.server.SimpleHTTPRequestHandler)
		break
	except:
		err = sys.exc_info()[1]
		time.sleep(i / 100)
if not httpd:
	raise err

with open('$HTTPS_PORT_FILE', 'w') as portf:
	print(port, file=portf)

sslctx = ssl.SSLContext(ssl.PROTOCOL_TLS_SERVER)
sslctx.check_hostname = False
sslctx.load_cert_chain(certfile='$SSL_CA_CERT_FILE', keyfile='/$TESTPOOL/snakeoil.key')
httpd.socket = httpd.socket = sslctx.wrap_socket(httpd.socket, server_side=True)

os.chdir('$STF_SUITE/tests/functional/cli_root/zfs_load-key')

with open('/$TESTPOOL/snakeoil.pid', 'w') as pidf:
	if os.fork() != 0:
	  os._exit(0)
	print(os.getpid(), file=pidf)

sys.stdout.close()
sys.stderr.close()
try:
	sys.stdout = sys.stderr = open('/tmp/ZTS-snakeoil.log', 'w', buffering=1) # line
except:
	sys.stdout = sys.stderr = open('/dev/null', 'w')

print('{} start on {}'.format(os.getpid(), port))
httpd.serve_forever()
" || log_fail

	typeset https_pid=
	for d in $(seq 0 0.1 5); do
		read -r https_pid 2>/dev/null < "/$TESTPOOL/snakeoil.pid" && [ -n "$https_pid" ] && break
		sleep "$d"
	done
	[ -z "$https_pid" ] && log_fail "Couldn't start HTTPS server"
	log_note "Started HTTPS server as $https_pid on port $(get_https_port)"
}

function cleanup_https
{
	typeset https_pid=
	read -r https_pid 2>/dev/null < "/$TESTPOOL/snakeoil.pid" || return 0

	log_must kill "$https_pid"
	cat /tmp/ZTS-snakeoil.log
	rm -f "/$TESTPOOL/snakeoil.pid" "/tmp/ZTS-snakeoil.log"
}