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

DEVOPS-3341 Update teleport version #137

Merged
merged 3 commits into from
Jun 26, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

TSH_VERSION = 10.3.15
TSH_VERSION = 13.0.3

# Build command binary, for macOS
build_macos:
Expand Down
28 changes: 18 additions & 10 deletions pkg/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ import (

var (
//go:embed bin/tsh
tsh []byte
tshMD5 string
tsh []byte
tshMD5 string
tshInstallDir = "/usr/local/bin/"
)

Expand All @@ -56,20 +56,28 @@ func InstallOrUpgradeTsh() error {

//Generate MD5 hash of installed tsh binary
f, err := os.Open(binPath)
if err != nil {
if err != nil {
return errors.Wrapf(err, "Error opening tsh")
}
defer f.Close()
}
defer f.Close()

hash := md5.New()
_, err = io.Copy(hash, f)
if err != nil {
hash := md5.New()
_, err = io.Copy(hash, f)
if err != nil {
return errors.Wrapf(err, "Error generating hash for tsh")
}
}
// Check if tsh binary's md5 is same; if not, install tsh
if hex.EncodeToString(hash.Sum(nil)) != GetTshMd5Hash() {
pterm.Info.Println("Tsh version not matched, re-installing using sudo...")
// First write tsh binary to tmp

// First remove old tsh binary
rm := []string{"rm", "-rf", binPath}
err = ExecuteCommand("sudo", rm, false)
if err != nil {
return err
}

// Now write tsh binary to tmp
err = os.WriteFile("/tmp/tsh", tsh, 0755)
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion pkg/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func getClientByContext(kubeconfig string, kubeContext *api.Context) (client.Cli
checkTSH := os.Getenv("RIDECTL_TSH_CHECK")

// Return error to skip searching non-ridecell hosts
if checkTSH != "false" && !strings.HasSuffix(cfg.Host, ":3026") {
if checkTSH != "false" && !strings.Contains(cfg.Host, "teleport") {
return nil, errors.New("hostname did not match, ignoring context")
}
}
Expand Down
47 changes: 42 additions & 5 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ import (
"io"
"net/http"
"os"
"reflect"
"path/filepath"
"reflect"
"strings"
"time"

"github.com/Ridecell/ridectl/pkg/exec"
"github.com/Ridecell/ridectl/pkg/kubernetes"
Expand Down Expand Up @@ -84,6 +85,8 @@ func CheckTshLogin() {
statusArgs := []string{"status"}
err = exec.ExecuteCommand("tsh", statusArgs, false)
if err == nil {
// Execute tsh kube login command for automatically populate kubeconfig
populateKubeConfig()
return
}
// check if no teleport profile present, ask user to login
Expand All @@ -94,6 +97,40 @@ func CheckTshLogin() {
os.Exit(1)
}

func populateKubeConfig() {
// Check last modified date of /tmp/ridectl-kube.log file
now := time.Now()
filename := "/tmp/ridectl-kube.log"
vivekjainx86 marked this conversation as resolved.
Show resolved Hide resolved
tmpFile, err := os.Stat(filename)
if err != nil {
// Executing tsh kube login
kubeLoginArgs := []string{"kube", "login", "--all"}
err = exec.ExecuteCommand("tsh", kubeLoginArgs, false)
if err != nil {
pterm.Warning.Printf("Error configuring kubernetes contexts: %s\n", err)
return
}
err = os.WriteFile(filename, []byte(""), 0644)
if err != nil {
pterm.Warning.Printf("Error creating temp file: %s\n", err)
}
return
}
// Check if file is modified in last 7 days
if now.Sub(tmpFile.ModTime()).Hours()/24 > 7 {
vivekjainx86 marked this conversation as resolved.
Show resolved Hide resolved
kubeLoginArgs := []string{"kube", "login", "--all"}
err = exec.ExecuteCommand("tsh", kubeLoginArgs, false)
if err != nil {
pterm.Warning.Printf("Error configuring kubernetes contexts: %s\n", err)
return
}
if err := os.Chtimes(filename, now, now); err != nil {
pterm.Warning.Printf("Error updating temp file stat: %s\n", err)
}
return
}
}

func DoesInstanceExist(name string, inCluster bool) (kubernetes.Subject, kubernetes.Kubeobject, bool) {
kubeconfig := GetKubeconfig()
target, err := kubernetes.ParseSubject(name)
Expand All @@ -110,10 +147,10 @@ func DoesInstanceExist(name string, inCluster bool) (kubernetes.Subject, kuberne
return target, kubeObj, false
}
if reflect.DeepEqual(kubeObj, kubernetes.Kubeobject{}) {
pterm.Error.Printf("No instance found [%s]. Double check the following:\n" +
"- Instance name is correct\n" +
"- You have the required access in Infra-Auth\n" +
"For more details and help with the above, see: https://docs.google.com/document/d/1v6lbH4NgN6rHBHpELWrcQ4CyqwVeSgeP/preview#heading=h.xq8mwj7wt9h1\n", name)
pterm.Error.Printf("No instance found [%s]. Double check the following:\n"+
"- Instance name is correct\n"+
"- You have the required access in Infra-Auth\n"+
"For more details and help with the above, see: https://docs.google.com/document/d/1v6lbH4NgN6rHBHpELWrcQ4CyqwVeSgeP/preview#heading=h.xq8mwj7wt9h1\n", name)
return target, kubeObj, false
}

Expand Down