tag v0.155.0 Tagger: imagebuilder-bot <imagebuilder-bots+imagebuilder-bot@redhat.com> Changes with 0.155.0 ---------------- * Fedora 43: add shadow-utils when LockRoot is enabled, update cloud-init service name (osbuild/images#1618) * Author: Achilleas Koutsou, Reviewers: Gianluca Zuccarelli, Michael Vogt * Update osbuild dependency commit ID to latest (osbuild/images#1609) * Author: SchutzBot, Reviewers: Achilleas Koutsou, Simon de Vlieger, Tomáš Hozza * Update snapshots to 20250626 (osbuild/images#1623) * Author: SchutzBot, Reviewers: Achilleas Koutsou, Simon de Vlieger * distro/rhel9: xz compress azure-cvm image type [HMS-8587] (osbuild/images#1620) * Author: Achilleas Koutsou, Reviewers: Simon de Vlieger, Tomáš Hozza * distro/rhel: introduce new image type: Azure SAP Apps [HMS-8738] (osbuild/images#1612) * Author: Achilleas Koutsou, Reviewers: Simon de Vlieger, Tomáš Hozza * distro/rhel: move ansible-core to sap_extras_pkgset (osbuild/images#1624) * Author: Achilleas Koutsou, Reviewers: Brian C. Lane, Tomáš Hozza * github/create-tag: allow passing the version when run manually (osbuild/images#1621) * Author: Achilleas Koutsou, Reviewers: Lukáš Zapletal, Tomáš Hozza * rhel9: move image-config into pure YAML (HMS-8593) (osbuild/images#1616) * Author: Michael Vogt, Reviewers: Achilleas Koutsou, Simon de Vlieger * test: split manifest checksums into separate files (osbuild/images#1625) * Author: Achilleas Koutsou, Reviewers: Simon de Vlieger, Tomáš Hozza — Somewhere on the Internet, 2025-06-30 --- tag v0.156.0 Tagger: imagebuilder-bot <imagebuilder-bots+imagebuilder-bot@redhat.com> Changes with 0.156.0 ---------------- * Many: delete repositories for EOL distributions (HMS-7044) (osbuild/images#1607) * Author: Tomáš Hozza, Reviewers: Michael Vogt, Simon de Vlieger * RHSM/facts: add 'image-builder CLI' API type (osbuild/images#1640) * Author: Tomáš Hozza, Reviewers: Brian C. Lane, Simon de Vlieger * Update dependencies 2025-06-29 (osbuild/images#1628) * Author: SchutzBot, Reviewers: Simon de Vlieger, Tomáš Hozza * Update osbuild dependency commit ID to latest (osbuild/images#1627) * Author: SchutzBot, Reviewers: Simon de Vlieger, Tomáš Hozza * [RFC] image: drop `InstallWeakDeps` from image.DiskImage (osbuild/images#1642) * Author: Michael Vogt, Reviewers: Brian C. Lane, Simon de Vlieger, Tomáš Hozza * build(deps): bump the go-deps group across 1 directory with 3 updates (osbuild/images#1632) * Author: dependabot[bot], Reviewers: SchutzBot, Tomáš Hozza * distro/rhel10: xz compress azure-cvm image type (osbuild/images#1638) * Author: Achilleas Koutsou, Reviewers: Brian C. Lane, Simon de Vlieger * distro: cleanup/refactor distro/{defs,generic} (HMS-8744) (osbuild/images#1570) * Author: Michael Vogt, Reviewers: Simon de Vlieger, Tomáš Hozza * distro: remove some hardcoded values from generic/images.go (osbuild/images#1636) * Author: Michael Vogt, Reviewers: Simon de Vlieger, Tomáš Hozza * distro: small tweaks for the YAML based imagetypes (osbuild/images#1622) * Author: Michael Vogt, Reviewers: Brian C. Lane, Simon de Vlieger * fedora/wsl: packages and locale (osbuild/images#1635) * Author: Simon de Vlieger, Reviewers: Michael Vogt, Tomáš Hozza * image/many: make compression more generic (osbuild/images#1634) * Author: Simon de Vlieger, Reviewers: Brian C. Lane, Michael Vogt * manifest: handle content template name with spaces (osbuild/images#1641) * Author: Bryttanie, Reviewers: Brian C. Lane, Michael Vogt, Tomáš Hozza * many: implement gzip (osbuild/images#1633) * Author: Simon de Vlieger, Reviewers: Michael Vogt, Tomáš Hozza * rhel/azure: set GRUB_TERMINAL based on architecture [RHEL-91383] (osbuild/images#1626) * Author: Achilleas Koutsou, Reviewers: Simon de Vlieger, Tomáš Hozza — Somewhere on the Internet, 2025-07-07 ---
312 lines
7.2 KiB
Go
312 lines
7.2 KiB
Go
// © Broadcom. All Rights Reserved.
|
|
// The term “Broadcom” refers to Broadcom Inc. and/or its subsidiaries.
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package session
|
|
|
|
import (
|
|
"context"
|
|
"net/url"
|
|
"os"
|
|
"strings"
|
|
"sync"
|
|
|
|
"github.com/vmware/govmomi/fault"
|
|
"github.com/vmware/govmomi/property"
|
|
"github.com/vmware/govmomi/vim25"
|
|
"github.com/vmware/govmomi/vim25/methods"
|
|
"github.com/vmware/govmomi/vim25/mo"
|
|
"github.com/vmware/govmomi/vim25/types"
|
|
)
|
|
|
|
// Locale defaults to "en_US" and can be overridden via this var or the GOVMOMI_LOCALE env var.
|
|
// A value of "_" uses the server locale setting.
|
|
var Locale = os.Getenv("GOVMOMI_LOCALE")
|
|
|
|
func init() {
|
|
if Locale == "_" {
|
|
Locale = ""
|
|
} else if Locale == "" {
|
|
Locale = "en_US"
|
|
}
|
|
}
|
|
|
|
// Secret returns the contents if a file path value is given, otherwise returns value itself.
|
|
func Secret(value string) (string, error) {
|
|
if len(value) == 0 {
|
|
return value, nil
|
|
}
|
|
contents, err := os.ReadFile(value)
|
|
if err != nil {
|
|
if os.IsPermission(err) {
|
|
return "", err
|
|
}
|
|
return value, nil
|
|
}
|
|
return strings.TrimSpace(string(contents)), nil
|
|
}
|
|
|
|
type Manager struct {
|
|
client *vim25.Client
|
|
userSession *types.UserSession
|
|
mu sync.Mutex
|
|
}
|
|
|
|
func NewManager(client *vim25.Client) *Manager {
|
|
m := Manager{
|
|
client: client,
|
|
}
|
|
|
|
return &m
|
|
}
|
|
|
|
func (sm *Manager) Reference() types.ManagedObjectReference {
|
|
return *sm.client.ServiceContent.SessionManager
|
|
}
|
|
|
|
func (sm *Manager) SetLocale(ctx context.Context, locale string) error {
|
|
req := types.SetLocale{
|
|
This: sm.Reference(),
|
|
Locale: locale,
|
|
}
|
|
|
|
_, err := methods.SetLocale(ctx, sm.client, &req)
|
|
return err
|
|
}
|
|
|
|
func (sm *Manager) setUserSession(val *types.UserSession) {
|
|
sm.mu.Lock()
|
|
sm.userSession = val
|
|
sm.mu.Unlock()
|
|
}
|
|
|
|
func (sm *Manager) getUserSession() (types.UserSession, bool) {
|
|
sm.mu.Lock()
|
|
defer sm.mu.Unlock()
|
|
if sm.userSession == nil {
|
|
return types.UserSession{}, false
|
|
}
|
|
return *sm.userSession, true
|
|
}
|
|
|
|
func (sm *Manager) Login(ctx context.Context, u *url.Userinfo) error {
|
|
req := types.Login{
|
|
This: sm.Reference(),
|
|
Locale: Locale,
|
|
}
|
|
|
|
if u != nil {
|
|
req.UserName = u.Username()
|
|
if pw, ok := u.Password(); ok {
|
|
req.Password = pw
|
|
}
|
|
}
|
|
|
|
login, err := methods.Login(ctx, sm.client, &req)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
sm.setUserSession(&login.Returnval)
|
|
return nil
|
|
}
|
|
|
|
// LoginExtensionByCertificate uses the vCenter SDK tunnel to login using a client certificate.
|
|
// The client certificate can be set using the soap.Client.SetCertificate method.
|
|
func (sm *Manager) LoginExtensionByCertificate(ctx context.Context, key string) error {
|
|
c := sm.client
|
|
u := c.URL()
|
|
if u.Hostname() != "sdkTunnel" {
|
|
sc := c.Tunnel()
|
|
c = &vim25.Client{
|
|
Client: sc,
|
|
RoundTripper: sc,
|
|
ServiceContent: c.ServiceContent,
|
|
}
|
|
// When http.Transport.Proxy is used, our thumbprint checker is bypassed, resulting in:
|
|
// "Post https://sdkTunnel:8089/sdk: x509: certificate is valid for $vcenter_hostname, not sdkTunnel"
|
|
// The only easy way around this is to disable verification for the call to LoginExtensionByCertificate().
|
|
// TODO: find a way to avoid disabling InsecureSkipVerify.
|
|
c.DefaultTransport().TLSClientConfig.InsecureSkipVerify = true
|
|
}
|
|
|
|
req := types.LoginExtensionByCertificate{
|
|
This: sm.Reference(),
|
|
ExtensionKey: key,
|
|
Locale: Locale,
|
|
}
|
|
|
|
login, err := methods.LoginExtensionByCertificate(ctx, c, &req)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// Copy the session cookie
|
|
sm.client.Jar.SetCookies(u, c.Jar.Cookies(c.URL()))
|
|
|
|
sm.setUserSession(&login.Returnval)
|
|
return nil
|
|
}
|
|
|
|
func (sm *Manager) LoginByToken(ctx context.Context) error {
|
|
req := types.LoginByToken{
|
|
This: sm.Reference(),
|
|
Locale: Locale,
|
|
}
|
|
|
|
login, err := methods.LoginByToken(ctx, sm.client, &req)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
sm.setUserSession(&login.Returnval)
|
|
return nil
|
|
}
|
|
|
|
func (sm *Manager) Logout(ctx context.Context) error {
|
|
req := types.Logout{
|
|
This: sm.Reference(),
|
|
}
|
|
|
|
_, err := methods.Logout(ctx, sm.client, &req)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
sm.setUserSession(nil)
|
|
return nil
|
|
}
|
|
|
|
// UserSession retrieves and returns the SessionManager's CurrentSession field.
|
|
// Nil is returned if the session is not authenticated.
|
|
func (sm *Manager) UserSession(ctx context.Context) (*types.UserSession, error) {
|
|
var mgr mo.SessionManager
|
|
|
|
pc := property.DefaultCollector(sm.client)
|
|
err := pc.RetrieveOne(ctx, sm.Reference(), []string{"currentSession"}, &mgr)
|
|
if err != nil {
|
|
// It's OK if we can't retrieve properties because we're not authenticated
|
|
if fault.Is(err, &types.NotAuthenticated{}) {
|
|
return nil, nil
|
|
}
|
|
|
|
return nil, err
|
|
}
|
|
|
|
return mgr.CurrentSession, nil
|
|
}
|
|
|
|
func (sm *Manager) TerminateSession(ctx context.Context, sessionId []string) error {
|
|
req := types.TerminateSession{
|
|
This: sm.Reference(),
|
|
SessionId: sessionId,
|
|
}
|
|
|
|
_, err := methods.TerminateSession(ctx, sm.client, &req)
|
|
return err
|
|
}
|
|
|
|
// SessionIsActive checks whether the session that was created at login is
|
|
// still valid. This function only works against vCenter.
|
|
func (sm *Manager) SessionIsActive(ctx context.Context) (bool, error) {
|
|
userSession, ok := sm.getUserSession()
|
|
if !ok {
|
|
return false, nil
|
|
}
|
|
|
|
req := types.SessionIsActive{
|
|
This: sm.Reference(),
|
|
SessionID: userSession.Key,
|
|
UserName: userSession.UserName,
|
|
}
|
|
|
|
active, err := methods.SessionIsActive(ctx, sm.client, &req)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
|
|
return active.Returnval, err
|
|
}
|
|
|
|
func (sm *Manager) AcquireGenericServiceTicket(ctx context.Context, spec types.BaseSessionManagerServiceRequestSpec) (*types.SessionManagerGenericServiceTicket, error) {
|
|
req := types.AcquireGenericServiceTicket{
|
|
This: sm.Reference(),
|
|
Spec: spec,
|
|
}
|
|
|
|
res, err := methods.AcquireGenericServiceTicket(ctx, sm.client, &req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &res.Returnval, nil
|
|
}
|
|
|
|
func (sm *Manager) AcquireLocalTicket(ctx context.Context, userName string) (*types.SessionManagerLocalTicket, error) {
|
|
req := types.AcquireLocalTicket{
|
|
This: sm.Reference(),
|
|
UserName: userName,
|
|
}
|
|
|
|
res, err := methods.AcquireLocalTicket(ctx, sm.client, &req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &res.Returnval, nil
|
|
}
|
|
|
|
func (sm *Manager) AcquireCloneTicket(ctx context.Context) (string, error) {
|
|
req := types.AcquireCloneTicket{
|
|
This: sm.Reference(),
|
|
}
|
|
|
|
res, err := methods.AcquireCloneTicket(ctx, sm.client, &req)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
return res.Returnval, nil
|
|
}
|
|
|
|
func (sm *Manager) CloneSession(ctx context.Context, ticket string) error {
|
|
req := types.CloneSession{
|
|
This: sm.Reference(),
|
|
CloneTicket: ticket,
|
|
}
|
|
|
|
res, err := methods.CloneSession(ctx, sm.client, &req)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
sm.setUserSession(&res.Returnval)
|
|
return nil
|
|
}
|
|
|
|
func (sm *Manager) UpdateServiceMessage(ctx context.Context, message string) error {
|
|
req := types.UpdateServiceMessage{
|
|
This: sm.Reference(),
|
|
Message: message,
|
|
}
|
|
|
|
_, err := methods.UpdateServiceMessage(ctx, sm.client, &req)
|
|
|
|
return err
|
|
}
|
|
|
|
func (sm *Manager) ImpersonateUser(ctx context.Context, name string) error {
|
|
req := types.ImpersonateUser{
|
|
This: sm.Reference(),
|
|
UserName: name,
|
|
Locale: Locale,
|
|
}
|
|
|
|
res, err := methods.ImpersonateUser(ctx, sm.client, &req)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
sm.setUserSession(&res.Returnval)
|
|
return nil
|
|
}
|