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

v1.0.0 #798

Draft
wants to merge 197 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
197 commits
Select commit Hold shift + click to select a range
3ddbcbd
changelog placeholder for 1.0.0
michaelquigley Aug 7, 2024
8863ea1
'zrok daemon' cli skeleton (#463)
michaelquigley Aug 12, 2024
a682482
Merge branch 'main' into v1_0_0
michaelquigley Aug 20, 2024
09e2d2a
Merge branch 'v1_0_0' into daemon_mode
michaelquigley Aug 20, 2024
2d33c99
daemon model skeleton (#463)
michaelquigley Aug 21, 2024
83ad5dd
daemon -> agent (#463)
michaelquigley Aug 22, 2024
bd0f2d7
grpc infrastructure (#463)
michaelquigley Aug 23, 2024
95098e6
roughed in grpc infrastructure for agent (#463)
michaelquigley Aug 23, 2024
3c310c2
'zrok agent' -> 'zrok agent start' (#463)
michaelquigley Aug 23, 2024
896125d
version client (#463)
michaelquigley Aug 23, 2024
23796be
package naming housekeeping (#463)
michaelquigley Aug 23, 2024
3ba1796
shutdown hook to clean up agent at exit (#463)
michaelquigley Aug 23, 2024
1a6d6e5
Merge branch 'main' into v1_0_0
michaelquigley Aug 23, 2024
931d101
Merge branch 'main' into daemon_mode
michaelquigley Aug 23, 2024
c0a5afc
Merge branch 'v1_0_0' into daemon_mode
michaelquigley Aug 23, 2024
d8c9681
tweak (#463)
michaelquigley Aug 23, 2024
1808747
windows path handling
michaelquigley Aug 23, 2024
64c984f
better grpc dialing for windows (#463)
michaelquigley Aug 23, 2024
a89d8ef
more logical grpc dialing (#463)
michaelquigley Aug 23, 2024
84c7e26
'proto3' (#463)
michaelquigley Aug 26, 2024
e0592a3
factor out unix domain grpc client (#463)
michaelquigley Aug 26, 2024
66a95f0
rudimentary 'zrok agent status' messages (#463)
michaelquigley Aug 26, 2024
b69025c
rudimentary 'zrok agent status' implementation (#463)
michaelquigley Aug 26, 2024
a9bac04
most minimal public sharing (proxy only) to elaborate plumbing (#463)
michaelquigley Aug 26, 2024
a1c3f80
Merge branch 'main' into v1_0_0
michaelquigley Aug 27, 2024
6a8d41e
Merge branch 'v1_0_0' into daemon_mode
michaelquigley Aug 27, 2024
6549130
Merge branch 'main' into v1_0_0
michaelquigley Sep 4, 2024
2699e0d
Merge branch 'v1_0_0' into daemon_mode
michaelquigley Sep 4, 2024
2bef728
Merge branch 'main' into v1_0_0
michaelquigley Sep 6, 2024
3f2e554
Merge branch 'v1_0_0' into daemon_mode
michaelquigley Sep 6, 2024
e585245
scaffolding for 'zrok agent release share' (#463)
michaelquigley Sep 10, 2024
e7405cd
stop the backend handler (#463)
michaelquigley Sep 10, 2024
597776a
roughed in share release (#463)
michaelquigley Sep 10, 2024
82d8f4b
web shares (#463)
michaelquigley Sep 10, 2024
42c3ec4
rough proctree implementation (#748)
michaelquigley Sep 12, 2024
cf7cce0
removed previous agent innards (#748)
michaelquigley Sep 12, 2024
56c58ee
minimal sub-process based agent implementatio (#748)
michaelquigley Sep 12, 2024
d26de73
return frontend endpoints, also (#748)
michaelquigley Sep 12, 2024
054788c
tail function (#748)
michaelquigley Sep 12, 2024
8525348
lint (#463)
michaelquigley Sep 12, 2024
2cf484e
better share lifecycle monitoring and error handling (#463)
michaelquigley Sep 13, 2024
9a90915
better file names (#463)
michaelquigley Sep 13, 2024
70d5fd7
target handling (#463)
michaelquigley Sep 13, 2024
5f81992
emit the '--agent' bootstrap information as soon as its available (#463)
michaelquigley Sep 13, 2024
9653980
agent shutdown lifecycle management improvements (#463, #748)
michaelquigley Sep 16, 2024
7747c3a
log lint (#463)
michaelquigley Sep 16, 2024
14719c1
ensure that proctree is fully configured (for windows) (#748)
michaelquigley Sep 16, 2024
254251a
updated grpc for private shares (#463)
michaelquigley Sep 16, 2024
dffb783
infrastructure for private sharing commands (#463)
michaelquigley Sep 16, 2024
9881401
support '--agent' for 'zrok share private' (#463)
michaelquigley Sep 16, 2024
9d829c1
private access wiring (#463)
michaelquigley Sep 17, 2024
e6a74ad
'--agent' for 'zrok access private' (#748)
michaelquigley Sep 17, 2024
fb23d23
working 'zrok agent access private' (#463)
michaelquigley Sep 17, 2024
9bf5ff5
better access listing in agent status (#463)
michaelquigley Sep 17, 2024
fa8c39b
grpc for 'zrok agent release access' (#463)
michaelquigley Sep 17, 2024
ccfe0df
'zrok agent release access' (#463)
michaelquigley Sep 17, 2024
35fd572
Merge branch 'main' into v1_0_0
michaelquigley Sep 17, 2024
abbd08b
Merge branch 'v1_0_0' into daemon_mode
michaelquigley Sep 17, 2024
e323107
cli bits for 'zrok agent share reserved' (#463)
michaelquigley Sep 17, 2024
cd253e4
'--agent' mode for 'zrok share reserved' (#463, #748)
michaelquigley Sep 17, 2024
df65230
reserved sharing infrastructure in the agent (#463)
michaelquigley Sep 17, 2024
7244bda
logging tweaks in 'zrok share reserved --agent' (#463)
michaelquigley Sep 17, 2024
edf40ca
all snake case (#463)
michaelquigley Sep 17, 2024
0a0d640
naming lint (#463)
michaelquigley Sep 18, 2024
d6a07b1
changelog (#463)
michaelquigley Sep 18, 2024
afbf5a1
v1.0.x series
michaelquigley Sep 18, 2024
e97620d
openapi spec 0.3.0 -> 1.0.0
michaelquigley Sep 18, 2024
37ed76e
Merge branch 'v1_0_0' into daemon_mode
michaelquigley Sep 18, 2024
5af4aa6
Merge pull request #739 from openziti/daemon_mode
michaelquigley Sep 18, 2024
e5ed124
rudimentary implementation of 'zrok share reserved' using dual-pathed…
michaelquigley Sep 20, 2024
05e0e3a
roughed-in dual-pathed cli implementation for 'zrok share [public|pri…
michaelquigley Sep 23, 2024
2163f02
add 'zrok access private' to dual-path cli (#751)
michaelquigley Sep 23, 2024
9cf5787
adjust 'proctree' to use SIGINT instead of SIGKILL (#748)
michaelquigley Sep 24, 2024
123a4ef
back out the SIGINT attempt (#748)
michaelquigley Sep 24, 2024
e0ca6e4
agent cleanup after process.Kill() (#748)
michaelquigley Sep 24, 2024
1d012b8
cleaned up removal path in the agent (#463)
michaelquigley Sep 25, 2024
9492f93
code polish and naming lint (#463)
michaelquigley Sep 25, 2024
3fbcaa5
rename '--agent' flag to '--subordinate' (#748)
michaelquigley Sep 25, 2024
f1200ee
logging lint (#463)
michaelquigley Sep 25, 2024
b49d70b
add '--force-local' and '--force-agent' to 'zrok share [public|privat…
michaelquigley Sep 25, 2024
8252ac6
changelog
michaelquigley Sep 25, 2024
f07d181
Merge branch 'main' into v1_0_0
michaelquigley Sep 25, 2024
e429556
Merge branch 'v1_0_0' into dual_pathed
michaelquigley Sep 25, 2024
73b3203
Merge pull request #756 from openziti/dual_pathed
michaelquigley Sep 25, 2024
b1daabf
add new 'headless' config option (#224)
michaelquigley Sep 26, 2024
f693a89
Merge branch 'main' into v1_0_0
michaelquigley Sep 30, 2024
10121b8
Merge branch 'v1_0_0' into more_config
michaelquigley Sep 30, 2024
70f5a23
Merge pull request #758 from openziti/more_config
michaelquigley Sep 30, 2024
6942408
initial grpc-gateway integration (#759)
michaelquigley Oct 1, 2024
0baf1dd
integrated openapi generation (#759)
michaelquigley Oct 1, 2024
c3fafec
next.js skeleton for agent ui (#221)
michaelquigley Oct 1, 2024
b787c6c
rough wiring for the grpc gateway (#221)
michaelquigley Oct 1, 2024
f7746bb
strip of the boilerplate (#221)
michaelquigley Oct 1, 2024
433290d
agent-ui rest client generation; initial version endpoint call (#221)
michaelquigley Oct 2, 2024
eebc6ef
table components and dynamic refresh for mvp (#221)
michaelquigley Oct 2, 2024
217f096
Merge branch 'main' into v1_0_0
michaelquigley Oct 3, 2024
f168091
Merge branch 'v1_0_0' into agent_ui_1
michaelquigley Oct 3, 2024
24ed2ec
next.js -> vite.js (#221)
michaelquigley Oct 4, 2024
99a0eea
both production and development builds now working with vite.js (#221)
michaelquigley Oct 4, 2024
5d57087
add all share/access overview data (#221)
michaelquigley Oct 4, 2024
3f00bb6
sort shares/accesses in status call (#221)
michaelquigley Oct 4, 2024
8bdf4f0
fix dependency (#221)
michaelquigley Oct 4, 2024
62e75e9
add vite build for agent ui to ci (#221)
michaelquigley Oct 4, 2024
03c2f1e
vite build refactoring (#221)
michaelquigley Oct 4, 2024
5d8da3c
update middleware for agent ui to work properly on windows (#221)
michaelquigley Oct 4, 2024
6bcd569
agent console iteration (#221)
michaelquigley Oct 4, 2024
b0178a0
zrok agent console (#221)
michaelquigley Oct 4, 2024
d3568c0
wired in console endpoint into agent, wired into 'zrok agent console'…
michaelquigley Oct 4, 2024
4749411
logging and other lint
michaelquigley Oct 7, 2024
a67dba0
toolbar (#221)
michaelquigley Oct 8, 2024
c5c5bb0
react router (#221)
michaelquigley Oct 8, 2024
f0cd66d
route parmeters (#221)
michaelquigley Oct 8, 2024
c02b9c2
centralized state (#221)
michaelquigley Oct 9, 2024
6eb60fc
centralized data retrieval (#221)
michaelquigley Oct 9, 2024
258980c
roughed-in cards (#221)
michaelquigley Oct 9, 2024
05aa12e
roughed in share and release through the agent ui (#221)
michaelquigley Oct 10, 2024
a9fa54c
Merge branch 'main' into v1_0_0
michaelquigley Oct 11, 2024
c424190
Merge branch 'v1_0_0' into agent_ui_1
michaelquigley Oct 11, 2024
a9abb8f
zrok office hours - zrok agent ui development (#221)
michaelquigley Oct 11, 2024
b94b609
Merge branch 'main' into v1_0_0
michaelquigley Oct 16, 2024
22cd4fa
Merge branch 'v1_0_0' into agent_ui_1
michaelquigley Oct 16, 2024
3059c3e
Merge branch 'main' into v1_0_0
michaelquigley Oct 18, 2024
aa24e8b
Merge branch 'v1_0_0' into agent_ui_1
michaelquigley Oct 18, 2024
becf2ee
Merge branch 'main' into v1_0_0
michaelquigley Oct 18, 2024
85f660d
lint
michaelquigley Oct 18, 2024
0c52aad
Merge branch 'v1_0_0' into agent_ui_1
michaelquigley Oct 18, 2024
aa5810a
initial mui theme customization (#221)
michaelquigley Oct 21, 2024
28e8896
more mui (#221)
michaelquigley Oct 21, 2024
b9609f1
theme component customization (#221)
michaelquigley Oct 21, 2024
7571a38
mui styling (#221)
michaelquigley Oct 21, 2024
b40e692
branding (#221)
michaelquigley Oct 21, 2024
ba34f47
component model refactoring (#221)
michaelquigley Oct 21, 2024
fe97b6d
throwing things at the wall (#221)
michaelquigley Oct 23, 2024
492e53b
more random junk at the wall (#221)
michaelquigley Oct 23, 2024
9e167a9
Merge branch 'main' into v1_0_0
michaelquigley Oct 24, 2024
d2556b2
Merge branch 'v1_0_0' into agent_ui_1
michaelquigley Oct 24, 2024
9867067
de-leaked (#221)
michaelquigley Oct 28, 2024
c84b2a3
@mui/material/Grid2 (#221)
michaelquigley Oct 29, 2024
9a12ab4
responsive layout improvements (#221)
michaelquigley Oct 29, 2024
e34e661
handler tweaks (#221)
michaelquigley Oct 29, 2024
0a99e3b
oops (#221)
michaelquigley Oct 29, 2024
2a6ecc6
tweaks (#221)
michaelquigley Oct 29, 2024
472b397
new share form improvements (#221)
michaelquigley Oct 29, 2024
84c1b8c
agent console iteration (#221)
michaelquigley Oct 29, 2024
553c2fe
bigger (#221)
michaelquigley Oct 29, 2024
6f1c8e6
size (#221)
michaelquigley Oct 29, 2024
b747216
Merge branch 'main' into v1_0_0
michaelquigley Nov 5, 2024
475c939
Merge branch 'v1_0_0' into agent_ui_1
michaelquigley Nov 5, 2024
47295e3
Merge branch 'main' into v1_0_0
michaelquigley Nov 6, 2024
99a2e66
Merge branch 'v1_0_0' into agent_ui_1
michaelquigley Nov 6, 2024
fe08a19
basic ranged auto listener (#779)
michaelquigley Nov 6, 2024
a0b7416
endpoint -> address (#779)
michaelquigley Nov 6, 2024
e7331e8
move autolistener into util (#780)
michaelquigley Nov 7, 2024
bf6f795
'--auto' mode for 'zrok access private' (#780)
michaelquigley Nov 7, 2024
3f8e760
update agent protocol to support auto mode (#780)
michaelquigley Nov 7, 2024
c728ae8
support --auto mode for agent-based private shares (#780)
michaelquigley Nov 7, 2024
f00835d
changelog (#780)
michaelquigley Nov 7, 2024
1face3b
error handing for '--subordinate' in 'zrok access private' (#789)
michaelquigley Nov 8, 2024
662aa59
debugging lint
michaelquigley Nov 8, 2024
35fc32b
error handling dry (#789)
michaelquigley Nov 8, 2024
94cfba7
subordinate mode messaging improvements (#789)
michaelquigley Nov 12, 2024
f5601e3
reworked 'tail' function for subordinate access (#789)
michaelquigley Nov 12, 2024
ef5e654
subordinate protocol update for 'zrok share private' (#789)
michaelquigley Nov 12, 2024
bf3a470
update 'zrok share reserved' to latest subordinate protocol (#789)
michaelquigley Nov 12, 2024
133087f
Merge branch 'main' into v1_0_0
michaelquigley Nov 13, 2024
f4fa04e
Merge branch 'v1_0_0' into agent_ui_1
michaelquigley Nov 13, 2024
85a2295
refactored 'share.tail' for new subordinate boot protocol (#789)
michaelquigley Nov 13, 2024
6b4dbaa
use json logging when in subordinate mode (#789)
michaelquigley Nov 13, 2024
77eca93
first iteration of work on the subordinate process framework (#789)
michaelquigley Nov 13, 2024
9ab803b
Merge branch 'main' into v1_0_0
michaelquigley Nov 14, 2024
4398746
deps
michaelquigley Nov 14, 2024
1e89bad
Merge branch 'v1_0_0' into agent_ui_1
michaelquigley Nov 14, 2024
a41ed30
tweaks (#789)
michaelquigley Nov 14, 2024
5c4cf9b
migrated the access infrastructure in the agent over to the new 'subo…
michaelquigley Nov 14, 2024
3205b92
port the share infrastructure over to the subordinate framework (#789)
michaelquigley Nov 15, 2024
0bdbc9e
new typescript skeleton for agent ui (#781)
michaelquigley Nov 18, 2024
fe77e69
prune unused dependencies (#781)
michaelquigley Nov 18, 2024
32f661c
package lint (#221)
michaelquigley Nov 18, 2024
6a535a1
exercise the generated api (#781)
michaelquigley Nov 18, 2024
7377b02
skip typescript checking for production builds (for now) (#221)
michaelquigley Nov 18, 2024
c3a6ef1
more typescript porting (#221)
michaelquigley Nov 19, 2024
418a7a4
Merge branch 'main' into v1_0_0
michaelquigley Nov 19, 2024
7f9f610
Merge branch 'v1_0_0' into agent_ui_1
michaelquigley Nov 19, 2024
0f2418e
share and access cards ported (#221)
michaelquigley Nov 19, 2024
7944985
new share modal ported to typescript (#221)
michaelquigley Nov 19, 2024
5298326
everything ported to typescript (#221)
michaelquigley Nov 19, 2024
fe6b861
replaced the old javascript 'agentUi' with the new typescript 'agentU…
michaelquigley Nov 19, 2024
6f674db
idiomatic lint (#221)
michaelquigley Nov 20, 2024
569dfce
typography lint (#221)
michaelquigley Nov 20, 2024
2770090
better idiom (#221)
michaelquigley Nov 20, 2024
7e641a9
error handling lint (#221)
michaelquigley Nov 20, 2024
5a9ed02
changelog for desktop ui (#221)
michaelquigley Nov 20, 2024
786a4e7
last round of generator testing to make sure everything is dialed in …
michaelquigley Nov 20, 2024
639510b
Merge pull request #797 from openziti/agent_ui_1
michaelquigley Nov 20, 2024
d259485
Merge branch 'main' into v1_0_0
michaelquigley Dec 2, 2024
496b363
Merge branch 'main' into v1_0_0
michaelquigley Dec 12, 2024
be0bf95
Merge branch 'main' into v1_0_0
michaelquigley Jan 8, 2025
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
14 changes: 12 additions & 2 deletions .github/workflows/ci-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,29 @@ jobs:
- name: setup-node
uses: actions/setup-node@v4
with:
node-version: 18.x
node-version: 20.x

- name: install ui node modules
shell: bash
run: npm install
working-directory: ui

- name: build node ui
- name: build ui
shell: bash
run: npm run build
working-directory: ui
env:
CI: "true"

- name: install agent ui node modules
shell: bash
run: npm install
working-directory: agent/agentUi

- name: build agent ui
shell: bash
run: npm run build
working-directory: agent/agentUi

- name: go install
shell: bash
Expand Down
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
# CHANGELOG

## v1.0.0

MAJOR RELEASE: zrok reaches version 1.0.0!

FEATURE: New "zrok Agent", a background manager process for your zrok environments, which allows you to easily manage and work with multiple `zrok share` and `zrok access` processes. New `--subordinate` flag added to `zrok share [public|private|reserved]` and `zrok access private` to operate in a mode that allows an Agent to manage shares and accesses (https://github.com/openziti/zrok/issues/463)

FEATURE: New "zrok Agent UI" a web-based user interface for the zrok Agent, which allows creating and releasing shares and accesses through a web browser. This is just an initial chunk of the new Agent UI, and is considered a "minimum viable" version of this interface (https://github.com/openziti/zrok/issues/221)

FEATURE: `zrok share [public|private|reserved]` and `zrok access private` now auto-detect if the zrok Agent is running in an environment and will automatically service share and access requests through the Agent, rather than in-process if the Agent is running. If the Agent is not running, operation remains as it was in `v0.4.x` and the share or access is handled in-process. New `--force-agent` and `--force-local` flags exist to skip Agent detection and manually select an operating mode (https://github.com/openziti/zrok/issues/751)

FEATURE `zrok access private` supports a new `--auto` mode, which can automatically find an available open address/port to bind the frontend listener on. Also includes `--auto-address`, `--auto-start-port`, and `--auto-end-port` features with sensible defaults. Supported by both the agent and local operating modes (https://github.com/openziti/zrok/issues/780)

## v0.4.46

FEATURE: Linux service template for systemd user units (https://github.com/openziti/zrok/pull/818)
Expand Down
30 changes: 30 additions & 0 deletions agent/access.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package agent

import (
"github.com/michaelquigley/pfxlog"
"github.com/openziti/zrok/agent/proctree"
"github.com/openziti/zrok/cmd/zrok/subordinate"
)

type access struct {
frontendToken string
token string
bindAddress string
autoMode bool
autoAddress string
autoStartPort uint16
autoEndPort uint16
responseHeaders []string

process *proctree.Child
sub *subordinate.MessageHandler

agent *Agent
}

func (a *access) monitor() {
if err := proctree.WaitChild(a.process); err != nil {
pfxlog.ChannelLogger(a.token).Error(err)
}
a.agent.rmAccess <- a
}
93 changes: 93 additions & 0 deletions agent/accessPrivate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package agent

import (
"context"
"errors"
"fmt"
"github.com/openziti/zrok/agent/agentGrpc"
"github.com/openziti/zrok/agent/proctree"
"github.com/openziti/zrok/cmd/zrok/subordinate"
"github.com/openziti/zrok/environment"
"github.com/sirupsen/logrus"
"os"
)

func (i *agentGrpcImpl) AccessPrivate(_ context.Context, req *agentGrpc.AccessPrivateRequest) (*agentGrpc.AccessPrivateResponse, error) {
root, err := environment.LoadRoot()
if err != nil {
return nil, err
}

if !root.IsEnabled() {
return nil, errors.New("unable to load environment; did you 'zrok enable'?")
}

accCmd := []string{os.Args[0], "access", "private", "--subordinate", "-b", req.BindAddress, req.Token}
if req.AutoMode {
accCmd = append(accCmd, "--auto", "--auto-address", req.AutoAddress, "--auto-start-port", fmt.Sprintf("%v", req.AutoStartPort))
accCmd = append(accCmd, "--auto-end-port", fmt.Sprintf("%v", req.AutoEndPort))
}
logrus.Info(accCmd)

acc := &access{
token: req.Token,
bindAddress: req.BindAddress,
autoMode: req.AutoMode,
autoAddress: req.AutoAddress,
autoStartPort: uint16(req.AutoStartPort),
autoEndPort: uint16(req.AutoEndPort),
responseHeaders: req.ResponseHeaders,
sub: subordinate.NewMessageHandler(),
agent: i.agent,
}
acc.sub.MessageHandler = func(msg subordinate.Message) {
logrus.Info(msg)
}
var bootErr error
acc.sub.BootHandler = func(msgType string, msg subordinate.Message) {
switch msgType {
case subordinate.BootMessage:
if v, found := msg["frontend_token"]; found {
if str, ok := v.(string); ok {
acc.frontendToken = str
}
}
if v, found := msg["bind_address"]; found {
if sr, ok := v.(string); ok {
acc.bindAddress = sr
}
}

case subordinate.ErrorMessage:
if v, found := msg[subordinate.ErrorMessage]; found {
if str, ok := v.(string); ok {
bootErr = errors.New(str)
}
}
}
}
acc.sub.MalformedHandler = func(msg subordinate.Message) {
logrus.Error(msg)
}

logrus.Infof("executing '%v'", accCmd)

acc.process, err = proctree.StartChild(acc.sub.Tail, accCmd...)
if err != nil {
return nil, err
}

<-acc.sub.BootComplete

if bootErr == nil {
go acc.monitor()
i.agent.addAccess <- acc
return &agentGrpc.AccessPrivateResponse{FrontendToken: acc.frontendToken}, nil

} else {
if err := proctree.WaitChild(acc.process); err != nil {
logrus.Errorf("error joining: %v", err)
}
return nil, fmt.Errorf("unable to start access: %v", bootErr)
}
}
197 changes: 197 additions & 0 deletions agent/agent.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
package agent

import (
"context"
"github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
"github.com/openziti/zrok/agent/agentGrpc"
"github.com/openziti/zrok/agent/agentUi"
"github.com/openziti/zrok/agent/proctree"
"github.com/openziti/zrok/environment/env_core"
"github.com/openziti/zrok/sdk/golang/sdk"
"github.com/openziti/zrok/util"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"net"
"net/http"
"os"
)

type Agent struct {
cfg *AgentConfig
httpEndpoint string
root env_core.Root
agentSocket string
shares map[string]*share
addShare chan *share
rmShare chan *share
accesses map[string]*access
addAccess chan *access
rmAccess chan *access
}

func NewAgent(cfg *AgentConfig, root env_core.Root) (*Agent, error) {
if !root.IsEnabled() {
return nil, errors.Errorf("unable to load environment; did you 'zrok enable'?")
}
return &Agent{
cfg: cfg,
root: root,
shares: make(map[string]*share),
addShare: make(chan *share),
rmShare: make(chan *share),
accesses: make(map[string]*access),
addAccess: make(chan *access),
rmAccess: make(chan *access),
}, nil
}

func (a *Agent) Run() error {
logrus.Infof("started")

if err := proctree.Init("zrok Agent"); err != nil {
return err
}

agentSocket, err := a.root.AgentSocket()
if err != nil {
return err
}
l, err := net.Listen("unix", agentSocket)
if err != nil {
return err
}
a.agentSocket = agentSocket

go a.manager()
go a.gateway(a.cfg)

srv := grpc.NewServer()
agentGrpc.RegisterAgentServer(srv, &agentGrpcImpl{agent: a})
if err := srv.Serve(l); err != nil {
return err
}

return nil
}

func (a *Agent) Shutdown() {
logrus.Infof("stopping")

if err := os.Remove(a.agentSocket); err != nil {
logrus.Warnf("unable to remove agent socket: %v", err)
}
for _, shr := range a.shares {
logrus.Debugf("stopping share '%v'", shr.token)
a.rmShare <- shr
}
for _, acc := range a.accesses {
logrus.Debugf("stopping access '%v'", acc.token)
a.rmAccess <- acc
}
}

func (a *Agent) Config() *AgentConfig {
return a.cfg
}

func (a *Agent) gateway(cfg *AgentConfig) {
logrus.Info("started")
defer logrus.Warn("exited")

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

mux := runtime.NewServeMux()
opts := []grpc.DialOption{grpc.WithTransportCredentials(insecure.NewCredentials())}
endpoint := "unix:" + a.agentSocket
logrus.Debugf("endpoint: '%v'", endpoint)
if err := agentGrpc.RegisterAgentHandlerFromEndpoint(ctx, mux, "unix:"+a.agentSocket, opts); err != nil {
logrus.Fatalf("unable to register gateway: %v", err)
}

listener, err := util.AutoListener("tcp", cfg.ConsoleAddress, cfg.ConsoleStartPort, cfg.ConsoleEndPort)
if err != nil {
logrus.Fatalf("unable to create a listener: %v", err)
}
a.httpEndpoint = listener.Addr().String()

if err := http.Serve(listener, agentUi.Middleware(mux)); err != nil {
logrus.Error(err)
}
}

func (a *Agent) manager() {
logrus.Info("started")
defer logrus.Warn("exited")

for {
select {
case inShare := <-a.addShare:
logrus.Infof("adding new share '%v'", inShare.token)
a.shares[inShare.token] = inShare

case outShare := <-a.rmShare:
if shr, found := a.shares[outShare.token]; found {
logrus.Infof("removing share '%v'", shr.token)
if err := proctree.StopChild(shr.process); err != nil {
logrus.Errorf("error stopping share '%v': %v", shr.token, err)
}
if err := proctree.WaitChild(shr.process); err != nil {
logrus.Errorf("error joining share '%v': %v", shr.token, err)
}
if !shr.reserved {
if err := a.deleteShare(shr.token); err != nil {
logrus.Errorf("error deleting share '%v': %v", shr.token, err)
}
}
delete(a.shares, shr.token)
} else {
logrus.Debug("skipping unidentified (orphaned) share removal")
}

case inAccess := <-a.addAccess:
logrus.Infof("adding new access '%v'", inAccess.frontendToken)
a.accesses[inAccess.frontendToken] = inAccess

case outAccess := <-a.rmAccess:
if acc, found := a.accesses[outAccess.frontendToken]; found {
logrus.Infof("removing access '%v'", acc.frontendToken)
if err := proctree.StopChild(acc.process); err != nil {
logrus.Errorf("error stopping access '%v': %v", acc.frontendToken, err)
}
if err := proctree.WaitChild(acc.process); err != nil {
logrus.Errorf("error joining access '%v': %v", acc.frontendToken, err)
}
if err := a.deleteAccess(acc.token, acc.frontendToken); err != nil {
logrus.Errorf("error deleting access '%v': %v", acc.frontendToken, err)
}
delete(a.accesses, acc.frontendToken)
} else {
logrus.Debug("skipping unidentified (orphaned) access removal")
}
}
}
}

func (a *Agent) deleteShare(token string) error {
logrus.Debugf("deleting share '%v'", token)
if err := sdk.DeleteShare(a.root, &sdk.Share{Token: token}); err != nil {
return err
}
return nil
}

func (a *Agent) deleteAccess(token, frontendToken string) error {
logrus.Debugf("deleting access '%v'", frontendToken)
if err := sdk.DeleteAccess(a.root, &sdk.Access{Token: frontendToken, ShareToken: token}); err != nil {
return err
}
return nil
}

type agentGrpcImpl struct {
agentGrpc.UnimplementedAgentServer
agent *Agent
}
Loading
Loading