@@ -23,6 +23,7 @@ import (
2323 "github.com/sylabs/singularity-mpi/internal/pkg/implem"
2424 "github.com/sylabs/singularity-mpi/internal/pkg/kv"
2525 "github.com/sylabs/singularity-mpi/internal/pkg/manifest"
26+ "github.com/sylabs/singularity-mpi/internal/pkg/syexec"
2627 "github.com/sylabs/singularity-mpi/internal/pkg/sys"
2728 util "github.com/sylabs/singularity-mpi/internal/pkg/util/file"
2829)
@@ -243,26 +244,22 @@ func Configure(env *buildenv.Info, sysCfg *sys.Config, extraArgs []string) error
243244 return fmt .Errorf ("failed to create %s: %s" , env .InstallDir , err )
244245 }
245246 }
246- singularityManifestPath := filepath .Join (env .InstallDir , "install.MANIFEST" )
247- err := manifest .Create (singularityManifestPath , []string {strings .Join (args , " " )})
248- if err != nil {
249- return fmt .Errorf ("failed to create installation manifest: %s" , err )
250- }
251247
252248 // Run mconfig
249+ var sycmd syexec.SyCmd
250+ sycmd .Env = updateEnviron (env )
251+ sycmd .ExecDir = env .SrcDir
252+ sycmd .ManifestDir = env .InstallDir
253+ sycmd .ManifestName = "mconfig"
254+ sycmd .BinPath = "./mconfig"
255+ sycmd .CmdArgs = args
256+ sycmd .ManifestData = []string {strings .Join (args , " " )}
253257 log .Printf ("-> Executing from %s: ./mconfig %s\n " , env .SrcDir , strings .Join (args , " " ))
254- newEnv := updateEnviron (env )
255- env .Env = newEnv
256- log .Printf ("-> Using env: %s\n " , strings .Join (newEnv , "\n " ))
257- var stderr bytes.Buffer
258- cmd = exec .CommandContext (ctx , "./mconfig" , args ... )
259- cmd .Dir = env .SrcDir
260- cmd .Env = newEnv
261- cmd .Stderr = & stderr
262- cmd .Stdout = & stdout
263- err = cmd .Run ()
264- if err != nil {
265- return fmt .Errorf ("failed to run mconfig: %s (stderr: %s; stdout: %s)" , err , stderr .String (), stdout .String ())
258+ log .Printf ("-> Using env: %s\n " , strings .Join (sycmd .Env , "\n " ))
259+
260+ res := sycmd .Run ()
261+ if res .Err != nil {
262+ return fmt .Errorf ("failed to run mconfig: %s (stderr: %s; stdout: %s)" , res .Err , res .Stderr , res .Stdout )
266263 }
267264
268265 return nil
@@ -362,3 +359,40 @@ func GetVersion(sysCfg *sys.Config) string {
362359
363360 return stdout .String ()
364361}
362+
363+ // CheckIntegrity checks if the installation of Singularity has been compromised
364+ func CheckIntegrity (sysCfg * sys.Config ) error {
365+ log .Println ("* Checking intergrity of Singularity..." )
366+
367+ if sysCfg .SingularityBin == "" {
368+ return fmt .Errorf ("singularity bianry cannot be found" )
369+ }
370+
371+ basedir := filepath .Dir (sysCfg .SingularityBin )
372+ basedir = filepath .Join (basedir , ".." )
373+ installManifest := filepath .Join (basedir , "singularity.MANIFEST" )
374+
375+ if util .FileExists (installManifest ) {
376+ data , err := ioutil .ReadFile (installManifest )
377+ if err != nil {
378+ log .Printf ("Manifest %s does not exist" , installManifest )
379+ return nil // This is not a fatal error
380+ }
381+ content := string (data )
382+ lines := strings .Split (content , "\n " )
383+ for _ , line := range lines {
384+ if strings .Contains (line , "bin/singularity: " ) {
385+ curFileHash := manifest .HashFiles ([]string {sysCfg .SingularityBin })
386+ if curFileHash [0 ] != line {
387+ hashRecordeed := strings .Split (line , ": " )[1 ]
388+ actualHash := strings .Split (curFileHash [0 ], ": " )[1 ]
389+ return fmt .Errorf ("hashes differ (record: %s; actual: %s)" , hashRecordeed , actualHash )
390+ }
391+ }
392+ }
393+ } else {
394+ log .Printf ("No manifest in %s, skipping...\n " , basedir )
395+ }
396+
397+ return nil
398+ }
0 commit comments