Skip to content

Commit

Permalink
deps(i18n): Move to external go-i18n and gotext (#886)
Browse files Browse the repository at this point in the history
We did the work to avoid our internal/ package last cycle. Move now to
it.
We still need to validate each lines that we log or not.

This removes the need for snapcore go-gettext.

UDENG-1990
  • Loading branch information
didrocks authored Jan 3, 2024
2 parents 3c4da75 + 06b6526 commit 7ea2aa1
Show file tree
Hide file tree
Showing 50 changed files with 467 additions and 1,029 deletions.
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,13 @@ require (
github.com/mvo5/libsmbclient-go v0.0.0-20220607104205-b69795f58cd0
github.com/pkg/sftp v1.13.6
github.com/sirupsen/logrus v1.9.3
github.com/snapcore/go-gettext v0.0.0-20191107141714-82bbea49e785
github.com/spf13/cobra v1.8.0
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.17.0
github.com/stretchr/testify v1.8.4
github.com/termie/go-shutil v0.0.0-20140729215957-bcacb06fecae
github.com/ubuntu/decorate v0.0.0-20230125165522-2d5b0a9bb117
github.com/ubuntu/go-i18n v0.0.0-20230830081132-d6654b958899
github.com/ubuntu/go-i18n v0.0.0-20231113092927-594c1754ca47
golang.org/x/crypto v0.17.0
golang.org/x/exp v0.0.0-20230905200255-921286631fa9
golang.org/x/net v0.19.0
Expand Down
6 changes: 2 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -244,8 +244,6 @@ github.com/sagikazarmark/slog-shim v0.1.0 h1:diDBnUNK9N/354PgrxMywXnAwEr1QZcOr6g
github.com/sagikazarmark/slog-shim v0.1.0/go.mod h1:SrcSrq8aKtyuqEI1uvTDTK1arOWRIczQRv+GVI1AkeQ=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/snapcore/go-gettext v0.0.0-20191107141714-82bbea49e785 h1:PaunR+BhraKSLxt2awQ42zofkP+NKh/VjQ0PjIMk/y4=
github.com/snapcore/go-gettext v0.0.0-20191107141714-82bbea49e785/go.mod h1:D3SsWAXK7wCCBZu+Vk5hc1EuKj/L3XN1puEMXTU4LrQ=
github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo=
github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0=
github.com/spf13/afero v1.10.0 h1:EaGW2JJh15aKOejeuJ+wpFSHnbd7GE6Wvp3TsNhb6LY=
Expand Down Expand Up @@ -275,8 +273,8 @@ github.com/termie/go-shutil v0.0.0-20140729215957-bcacb06fecae h1:vgGSvdW5Lqg+I1
github.com/termie/go-shutil v0.0.0-20140729215957-bcacb06fecae/go.mod h1:quDq6Se6jlGwiIKia/itDZxqC5rj6/8OdFyMMAwTxCs=
github.com/ubuntu/decorate v0.0.0-20230125165522-2d5b0a9bb117 h1:XQpsQG5lqRJlx4mUVHcJvyyc1rdTI9nHvwrdfcuy8aM=
github.com/ubuntu/decorate v0.0.0-20230125165522-2d5b0a9bb117/go.mod h1:mx0TjbqsaDD9DUT5gA1s3hw47U6RIbbIBfvGzR85K0g=
github.com/ubuntu/go-i18n v0.0.0-20230830081132-d6654b958899 h1:FquZaRc8X0+ImXSHKrqb4hdzcvlCHhXDNLjxzkwit6Y=
github.com/ubuntu/go-i18n v0.0.0-20230830081132-d6654b958899/go.mod h1:ZRhdDyx6YkKz/YiMWi0gS3uMCltgdaKz9IpkiNf/GRg=
github.com/ubuntu/go-i18n v0.0.0-20231113092927-594c1754ca47 h1:CA2dVorxvzdsGtszqhSjyvkrXxZi4bS52ZKvP0Ko634=
github.com/ubuntu/go-i18n v0.0.0-20231113092927-594c1754ca47/go.mod h1:ZRhdDyx6YkKz/YiMWi0gS3uMCltgdaKz9IpkiNf/GRg=
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
Expand Down
48 changes: 24 additions & 24 deletions internal/ad/ad.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ import (
"sync"
"time"

"github.com/leonelquinteros/gotext"
"github.com/ubuntu/adsys/internal/ad/backends"
adcommon "github.com/ubuntu/adsys/internal/ad/common"
"github.com/ubuntu/adsys/internal/ad/registry"
"github.com/ubuntu/adsys/internal/consts"
log "github.com/ubuntu/adsys/internal/grpc/logstreamer"
"github.com/ubuntu/adsys/internal/i18n"
"github.com/ubuntu/adsys/internal/policies"
"github.com/ubuntu/adsys/internal/policies/entry"
"github.com/ubuntu/adsys/internal/smbsafe"
Expand Down Expand Up @@ -117,7 +117,7 @@ var AdsysGpoListCode string

// New returns an AD object to manage concurrency, with a local kr5 ticket from machine keytab.
func New(ctx context.Context, configBackend backends.Backend, hostname string, opts ...Option) (ad *AD, err error) {
defer decorate.OnError(&err, i18n.G("can't create Active Directory object"))
defer decorate.OnError(&err, gotext.Get("can't create Active Directory object"))

versionID, err := adcommon.GetVersionID("/")
if err != nil {
Expand Down Expand Up @@ -155,7 +155,7 @@ func New(ctx context.Context, configBackend backends.Backend, hostname string, o
domain := configBackend.Domain()
serverFQDN, err := configBackend.ServerFQDN(ctx)
if err != nil && !errors.Is(err, backends.ErrNoActiveServer) {
return nil, fmt.Errorf(i18n.G("can't get current Server FQDN: %w"), err)
return nil, errors.New(gotext.Get("can't get current Server FQDN: %v", err))
}
log.Debugf(ctx, "Backend is SSSD. AD domain: %q, server from configuration: %q", domain, serverFQDN)

Expand All @@ -180,16 +180,16 @@ func New(ctx context.Context, configBackend backends.Backend, hostname string, o
// The GPOs are returned from the highest priority in the hierarchy, with enforcement in reverse order
// to the lowest priority.
func (ad *AD) GetPolicies(ctx context.Context, objectName string, objectClass ObjectClass, userKrb5CCName string) (pols policies.Policies, err error) {
defer decorate.OnError(&err, i18n.G("can't get policies for %q"), objectName)
defer decorate.OnError(&err, gotext.Get("can't get policies for %q", objectName))

log.Debugf(ctx, "GetPolicies for %q, type %q", objectName, objectClass)

if objectClass == UserObject && !strings.Contains(objectName, "@") {
return pols, fmt.Errorf(i18n.G("user name %q should be of the form %s@DOMAIN"), objectName, objectName)
return pols, errors.New(gotext.Get("user name %q should be of the form %s@DOMAIN", objectName, objectName))
}

if objectClass == ComputerObject && objectName != ad.hostname {
return pols, fmt.Errorf(i18n.G("requested a type computer of %q which isn't current host %q"), objectName, ad.hostname)
return pols, errors.New(gotext.Get("requested a type computer of %q which isn't current host %q", objectName, ad.hostname))
}

krb5CCPath := filepath.Join(ad.krb5CacheDir, objectName)
Expand Down Expand Up @@ -225,7 +225,7 @@ func (ad *AD) GetPolicies(ctx context.Context, objectName string, objectClass Ob
if !online {
var cachedPolicies policies.Policies
if cachedPolicies, err = policies.NewFromCache(ctx, filepath.Join(ad.policiesCacheDir, objectName)); err != nil {
return cachedPolicies, fmt.Errorf(i18n.G("machine is offline and policies cache is unavailable: %v"), err)
return cachedPolicies, errors.New(gotext.Get("machine is offline and policies cache is unavailable: %v", err))
}

log.Infof(ctx, "Can't reach AD: machine is offline and %q policies are applied using previous online update", objectName)
Expand All @@ -235,7 +235,7 @@ func (ad *AD) GetPolicies(ctx context.Context, objectName string, objectClass Ob
// We need an AD DC to connect to
adServerFQDN, err := ad.configBackend.ServerFQDN(ctx)
if err != nil {
return policies.Policies{}, fmt.Errorf(i18n.G("can't get current Server FQDN: %w"), err)
return policies.Policies{}, errors.New(gotext.Get("can't get current Server FQDN: %v", err))
}

// Otherwise, try fetching the GPO list from LDAP
Expand All @@ -256,7 +256,7 @@ func (ad *AD) GetPolicies(ctx context.Context, objectName string, objectClass Ob
err = cmd.Run()
smbsafe.DoneExec()
if err != nil {
return pols, fmt.Errorf(i18n.G("failed to retrieve the list of GPO (exited with %d): %v\n%s"), cmd.ProcessState.ExitCode(), err, stderr.String())
return pols, errors.New(gotext.Get("failed to retrieve the list of GPO (exited with %d): %v\n%s", cmd.ProcessState.ExitCode(), err, stderr.String()))
}

downloadables := make(map[string]string)
Expand Down Expand Up @@ -347,7 +347,7 @@ func (ad *AD) GetPolicies(ctx context.Context, objectName string, objectClass Ob
// ListUsers returns the list of users on the system based on their cached policy information.
// If active is true, the list of users is retrieved from the cached Kerberos ticket information.
func (ad *AD) ListUsers(ctx context.Context, active bool) (users []string, err error) {
defer decorate.OnError(&err, i18n.G("can't list users from cache"))
defer decorate.OnError(&err, gotext.Get("can't list users from cache"))

log.Debug(ctx, "ListUsers")

Expand All @@ -361,7 +361,7 @@ func (ad *AD) ListUsers(ctx context.Context, active bool) (users []string, err e

entries, err := os.ReadDir(cacheDir)
if err != nil {
return users, fmt.Errorf(i18n.G("failed to read cache directory: %v"), err)
return users, errors.New(gotext.Get("failed to read cache directory: %v", err))
}

for _, entry := range entries {
Expand Down Expand Up @@ -390,7 +390,7 @@ func (ad *AD) ensureKrb5CCSymlink(srcKrb5CCName, dstKrb5CCName string) error {

srcKrb5CCName, err := filepath.Abs(srcKrb5CCName)
if err != nil {
return fmt.Errorf(i18n.G("can't get absolute path of ccname to symlink to: %v"), err)
return errors.New(gotext.Get("can't get absolute path of ccname to symlink to: %v", err))
}

src, err := os.Readlink(dstKrb5CCName)
Expand All @@ -401,12 +401,12 @@ func (ad *AD) ensureKrb5CCSymlink(srcKrb5CCName, dstKrb5CCName string) error {
}
// Delete the symlink to create a new one.
if err := os.Remove(dstKrb5CCName); err != nil {
return fmt.Errorf(i18n.G("failed to remove existing symlink: %v"), err)
return errors.New(gotext.Get("failed to remove existing symlink: %v", err))
}
}

if err := os.MkdirAll(filepath.Dir(dstKrb5CCName), 0700); err != nil {
return fmt.Errorf(i18n.G("failed to create parent directory for symlink: %w"), err)
return errors.New(gotext.Get("failed to create parent directory for symlink: %v", err))
}

if err := os.Symlink(srcKrb5CCName, dstKrb5CCName); err != nil {
Expand All @@ -428,14 +428,14 @@ func (ad *AD) ensureKrb5CCCopy(krb5CCSymlink, krb5CCCopyName string) error {

krb5CCSrc, err := os.Readlink(krb5CCSymlink)
if err != nil {
return fmt.Errorf(i18n.G("failed to read krb5cc symlink: %w"), err)
return errors.New(gotext.Get("failed to read krb5cc symlink: %v", err))
}

if copyStat, err := os.Lstat(krb5CCCopyName); err == nil && copyStat.Mode()&os.ModeSymlink == 0 {
// We already have a copy of the ticket, let's check if we need to update it
srcStat, err := os.Stat(krb5CCSrc)
if err != nil {
return fmt.Errorf(i18n.G("failed to stat source ticket: %w"), err)
return errors.New(gotext.Get("failed to stat source ticket: %v", err))
}

// The source ticket is not newer than the destination one, no need to update
Expand All @@ -459,7 +459,7 @@ func (ad *AD) ensureKrb5CCCopy(krb5CCSymlink, krb5CCCopyName string) error {
func safeCopyFile(src, dst string, mode os.FileMode) error {
content, err := os.ReadFile(src)
if err != nil {
return fmt.Errorf(i18n.G("failed to read source file: %w"), err)
return errors.New(gotext.Get("failed to read source file: %v", err))
}
if err := os.WriteFile(dst+".new", content, mode); err != nil {
return err
Expand Down Expand Up @@ -522,7 +522,7 @@ func (ad *AD) parseGPOs(ctx context.Context, gpos []gpo, objectClass ObjectClass
// Decode and apply policies in gpo order. First win
pols, err := registry.DecodePolicy(f)
if err != nil {
return fmt.Errorf(i18n.G("%s: %v"), f.Name(), err)
return errors.New(gotext.Get("%s: %v", f.Name(), err))
}

// filter keys to be overridden
Expand All @@ -544,7 +544,7 @@ func (ad *AD) parseGPOs(ctx context.Context, gpos []gpo, objectClass ObjectClass
continue
}
if pol.Err != nil {
return fmt.Errorf(i18n.G("%s: %v"), f.Name(), pol.Err)
return errors.New(gotext.Get("%s: %v", f.Name(), pol.Err))
}
pol.Key = strings.TrimPrefix(pol.Key, keyFilterPrefix)

Expand Down Expand Up @@ -600,17 +600,17 @@ func (ad *AD) GetInfo(ctx context.Context) (msg string) {
var online string
if isOnline, err := ad.configBackend.IsOnline(); err != nil {
log.Warning(ctx, err)
online = fmt.Sprint(i18n.G("**Can't check if we have an active connection**\n"))
online = fmt.Sprint(gotext.Get("**Can't check if we have an active connection**\n"))
} else if !isOnline {
online = fmt.Sprint(i18n.G("**Offline mode** using cached policies\n"))
online = fmt.Sprint(gotext.Get("**Offline mode** using cached policies\n"))
}
domain := ad.configBackend.Domain()
server, err := ad.configBackend.ServerFQDN(ctx)
if err != nil {
server = "Unknown"
}

return fmt.Sprintf(i18n.G("%s\n%sDomain: %s\nServer FQDN: %s"), config, online, domain, server)
return gotext.Get("%s\n%sDomain: %s\nServer FQDN: %s", config, online, domain, server)
}

// NormalizeTargetName transforms the specified target to values adsys knows.
Expand Down Expand Up @@ -648,10 +648,10 @@ func (ad *AD) NormalizeTargetName(ctx context.Context, target string, objectClas
case 1:
baseUser = c[0]
default:
return "", fmt.Errorf(i18n.G(`only one \ is permitted in domain\username. Got: %s`), target)
return "", errors.New(gotext.Get(`only one \ is permitted in domain\username. Got: %s`, target))
}
if domainSuffix == "" && ad.configBackend.DefaultDomainSuffix() == "" {
return "", fmt.Errorf(i18n.G(`no domain provided for user %q and no default domain in sssd.conf`), target)
return "", errors.New(gotext.Get(`no domain provided for user %q and no default domain in sssd.conf`, target))
}
if domainSuffix == "" {
domainSuffix = ad.configBackend.DefaultDomainSuffix()
Expand Down
Loading

0 comments on commit 7ea2aa1

Please sign in to comment.