hide password if user/pass needed for packages repos
authorZhang Qiang <qiang.z.zhang@intel.com>
Wed, 20 Mar 2013 00:29:39 +0000 (20:29 -0400)
committerZhang Qiang <qiang.z.zhang@intel.com>
Thu, 3 Apr 2014 07:53:09 +0000 (15:53 +0800)
.repo.config is generated generated for passwd retrieving, and
.repo.config is saved to $BUILD_ROOT/ and remove it in build
cleanup stage.

fix #459, #390.

Change-Id: I2a5b3e23d504346414ee4b4dbb36ee0bbcb2e67a

build
download

diff --git a/build b/build
index 30c56e9..bc64a85 100755 (executable)
--- a/build
+++ b/build
@@ -377,6 +377,7 @@ usage () {
 cleanup_and_exit () {
     trap EXIT
     test -z "$1" && set 0
+    rm -f $BUILD_ROOT/.repo.config
     rm -f $BUILD_ROOT/exit
     if test "$1" -eq 1 -a -x /bin/df ; then
         # okay, it failed, but maybe because disk space?
@@ -1034,6 +1035,24 @@ check_for_arm()
     export VM_INITRD
 }
 
+hide_passwords()
+{
+    local i j
+    r=()
+    rm -f $BUILD_ROOT/.repo.config
+    for i in "${repos[@]}"; do
+        if [ "$i" == "--repo" -o "$i" == "--repository" ]; then
+            r=("${r[@]}" "$i")
+        else
+            echo $i |grep -E "^http[s]?:\/\/[^\/]*\/?" >/dev/null && echo $i >> $BUILD_ROOT/.repo.config
+            j=$(echo $i | sed -e "s#://[^@]*@#://#")
+            r=("${r[@]}" "$j")
+        fi
+    done
+    repos=("${r[@]}")
+    echo ${repos[@]}
+}
+
 check_for_ppc()
 {
     local uname
@@ -1941,6 +1960,7 @@ if test -z "$RUNNING_IN_VM" ; then
 fi
 
 mkdir_build_root
+hide_passwords
 
 if [ "$BUILD_ROOT" = / ]; then
     browner="$(stat -c %u /)"
index 9ac2bdf..4314617 100755 (executable)
--- a/download
+++ b/download
@@ -13,6 +13,12 @@ use File::Basename;
 
 use strict;
 
+sub hide_passwd {
+    my $url = shift;
+    $url =~ s|://[^@]*@|://|;
+    return $url
+}
+
 die "USAGE: $0 DIR URLS..." unless $#ARGV >= 1;
 
 my $dir = shift @ARGV;
@@ -22,6 +28,7 @@ my $ua = LWP::UserAgent->new(
   timeout => 42);
 
 for my $url (@ARGV) {
+  my $original = $url;
   if ($url =~ /^zypp:\/\/([^\/]*)\/?/) {
     use Build::Zypp;
     my $repo = Build::Zypp::parsecfg($1);
@@ -38,7 +45,27 @@ for my $url (@ARGV) {
       last;
     }
   } else {
-    $url = URI->new($url);
+    my $found = 0;
+    if ( defined $ENV{BUILD_ROOT} && -e $ENV{BUILD_ROOT} . "/.repo.config" ) {
+        open FILE, "<", $ENV{BUILD_ROOT} . "/.repo.config" or die $!;
+        while (<FILE>) {
+            next if ($_ !~ /^http[s]?:\/\/([^\/]*)\/?/);
+            chomp($_);
+            my $hidden = URI->new($_);
+            my $ui = $hidden->userinfo;
+            $hidden->userinfo(undef);
+            if ( $url =~ m/^$hidden/ ) {
+                $url = URI->new($url);
+                $url->userinfo($ui);
+                $found = 1;
+                last;
+            }
+        }
+        close FILE;
+    }
+    if ($found == 0 ) {
+        $url = URI->new($url);
+    }
   }
   $ua->env_proxy  if $url->scheme ne 'https';
   my $dest = "$dir/".basename($url->path);
@@ -48,8 +75,8 @@ for my $url (@ARGV) {
     my $res = $ua->mirror($url, $dest);
     last if $res->is_success;
     # if it's a redirect we probably got a bad mirror and should just retry
-    die "requesting $url failed: ".$res->status_line."\n" unless $retry && $res->previous;
-    warn "retrying $url\n";
+    die "reqesting " . hide_passwd($original) . " failed: ".$res->status_line."\n" unless $retry && $res->previous;
+    warn "retrying " . hide_passwd($original) . "\n";
   }
 }