Skip to content

Commit

Permalink
Merge pull request #7 from Bisnode/Forced-exit-on-timeout
Browse files Browse the repository at this point in the history
Force exit on timeout
  • Loading branch information
anderseknert authored May 29, 2020
2 parents e945f5b + a39b62c commit 9334a3b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.1.1] - 2020-05-29
### Changed
- kubectl-login will now exit after 10 minutes of idling. This in order to prevent the program from staying in the
background if left unattended.
- Added a sleep in the main loop to avoid hogging the CPU while active.

## [1.1.0] - 2020-02-12
### Changed
- Token is now stored outside of `KUBECONFIG` to avoid it being sent when expired, as described in the kubernetes issue
Expand Down
5 changes: 3 additions & 2 deletions handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"log"
"net/http"
"os"
"time"

"github.com/Bisnode/kubectl-login/util"
Expand Down Expand Up @@ -90,8 +91,8 @@ func (h *IDTokenWebhookHandler) ServeHTTP(w http.ResponseWriter, r *http.Request
}

if !h.ExecCredentialMode {
fmt.Println(fmt.Sprintf(
"Authenticated for context %v. Token valid until %v.", h.ClientCfg.CurrentContext, exp))
_, _ = fmt.Fprintf(os.Stdout,
"Authenticated for context %v. Token valid until %v.\n", h.ClientCfg.CurrentContext, exp)
}

// Return control to shell at this point
Expand Down
14 changes: 11 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func parseArgs(clientCfg *api.Config) (forceLogin bool, execCredentialMode bool,
}

if flag.NArg() > 0 {
_, _ = fmt.Fprint(os.Stderr, fmt.Sprintf("Unrecognized parameter(s): %v\n", flag.Args()))
_, _ = fmt.Fprintf(os.Stderr, "Unrecognized parameter(s): %v\n", flag.Args())
flag.Usage()
os.Exit(1)
}
Expand All @@ -135,6 +135,12 @@ func currentToken(clientCfg *api.Config) string {
func main() {
quitChan := make(chan struct{})
sigChan := make(chan os.Signal, 1)
timeoutChan := make(chan bool, 1)
go func() {
time.Sleep(10 * time.Minute)
timeoutChan <- true
}()

signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)

cluster := api.NewCluster()
Expand Down Expand Up @@ -181,8 +187,7 @@ func main() {
authzEndpointURL, _ := url.Parse(issuer.AuthorizeEndpoint)
_, err = net.LookupIP(authzEndpointURL.Host)
if err != nil {
fmt.Println(fmt.Sprintf("Could not resolve %v. Are you on the office network / VPN?", authzEndpointURL.Host))
os.Exit(1)
log.Fatalf("Could not resolve %v. Are you on the office network / VPN?", authzEndpointURL.Host)
}

nonce := util.RandomString(12)
Expand Down Expand Up @@ -239,7 +244,10 @@ func main() {
return
case <-sigChan:
close(quitChan)
case <-timeoutChan:
log.Fatal("kubetcl-login aborting after idling for 10 minutes")
default:
time.Sleep(100 * time.Millisecond)
}
}
}

0 comments on commit 9334a3b

Please sign in to comment.