コード例 #1
0
        public string GetSignature(Lease lease)
        {
            Lease lastLease = (from l in this.Leases orderby l.LeaseId descending select l).FirstOrDefault();

            StringWriter stringWriter = new StringWriter();
            stringWriter.Write(lastLease != null ? lastLease.HMAC : "");
            stringWriter.Write(';');
            lease.Write(stringWriter, false);


            using (HMAC hmac = HMAC.Create())
            {
                return Convert.ToBase64String(hmac.ComputeHash(Encoding.UTF8.GetBytes(stringWriter.ToString())));
            }
        }
コード例 #2
0
        private bool FixLease(Lease lease, DateTime time, bool fixEndTime = true)
        {
            PostSharp.Sdk.Extensibility.Licensing.License parsedLicense = ParsedLicenseManager.GetParsedLicense(lease.License.LicenseKey);

            if (lease.EndTime <= lease.StartTime)
            {
                throw new Exception("Assertion failed.");
            }

            if (fixEndTime)
            {
                if (parsedLicense.ValidTo.HasValue && parsedLicense.ValidTo < lease.EndTime)
                {
                    lease.EndTime = parsedLicense.ValidTo.Value;
                }

                if (lease.Grace)
                {
                    DateTime graceEnd = lease.License.GraceStartTime.Value.AddDays(parsedLicense.GetGraceDaysOrDefault());
                    if (lease.EndTime > graceEnd)
                    {
                        lease.EndTime = graceEnd;
                    }
                }


                if (lease.EndTime <= time)
                {
                    return(false);
                }

                if (lease.EndTime <= lease.StartTime)
                {
                    throw new Exception("Assertion failed.");
                }
            }

            lease.HMAC = this.GetSignature(lease);


            return(true);
        }
コード例 #3
0
        public LicenseLease GetLicenseLease(Database db, string productCode, Version version, DateTime?buildDate, string machine, string userName, string authenticatedUserName, DateTime now, Dictionary <int, string> errors)
        {
            Settings settings = Settings.Default;

            // Get suitable active licenses.
            License[] licenses = (from license in db.Licenses
                                  where license.ProductCode == productCode && license.Priority >= 0
                                  orderby license.Priority
                                  select license).ToArray();

            // Check if the machine belongs to build server user.
            if (IsBuildServer(machine))
            {
                License buildServerLicense = GetBuildServerLicense(licenses);
                if (buildServerLicense != null)
                {
                    DateTime     endTime = now.AddDays(Settings.Default.NewLeaseDays);
                    LicenseLease buildServerLicenseLease = new LicenseLease(buildServerLicense.LicenseKey, now, endTime, endTime.AddDays(-settings.MinLeaseDays));

                    // Lease for build server should not be stored - build server users shouldn't steal developer licenses.
                    return(buildServerLicenseLease);
                }

                // try to acquire lease in normal way
            }

            Lease lease = GetLease(db, productCode, version, buildDate, machine, userName, authenticatedUserName, now, errors, licenses);

            if (lease == null)
            {
                return(null);
            }

            LicenseLease licenseLease = new LicenseLease(lease.License.LicenseKey,
                                                         lease.StartTime, lease.EndTime, lease.EndTime.AddDays(-settings.MinLeaseDays));

            return(licenseLease);
        }
コード例 #4
0
        public Lease ProlongLease(Lease oldLease, string authenticatedUserName, DateTime time)
        {
            Lease newLease = new Lease
                                 {
                                     License = oldLease.License,
                                     AuthenticatedUser = authenticatedUserName,
                                     OverwritesLease = oldLease,
                                     StartTime = oldLease.StartTime,
                                     EndTime = time.AddDays(Settings.Default.NewLeaseDays),
                                     Machine = oldLease.Machine,
                                     UserName = oldLease.UserName,
                                     Grace = oldLease.Grace
                                 };

            if (!this.FixLease(newLease, time))
                return null;

            this.Leases.InsertOnSubmit(newLease);

            LeaseService.CheckAssertions( this, newLease.License, time );

            return newLease;
        }
コード例 #5
0
        public Lease CreateLease(License license, string user, string machine, string authenticatedUserName, DateTime time, bool grace)
        {
            Lease lease = new Lease
                              {
                                  License = license,
                                  AuthenticatedUser = authenticatedUserName,
                                  EndTime = time.AddDays(Settings.Default.NewLeaseDays),
                                  Machine = machine,
                                  StartTime = time,
                                  UserName = user,
                                  Grace = grace
                              };

            if (!this.FixLease(lease, time))
                return null;

            
            this.Leases.InsertOnSubmit((lease));

            LeaseService.CheckAssertions(this, license, time);


            return lease;
        }
コード例 #6
0
 partial void InsertLease(Lease instance);
コード例 #7
0
 partial void UpdateLease(Lease instance);
コード例 #8
0
		private void attach_Leases(Lease entity)
		{
			this.SendPropertyChanging();
			entity.License = this;
		}
コード例 #9
0
		private void detach_Leases(Lease entity)
		{
			this.SendPropertyChanging();
			entity.License = null;
		}
コード例 #10
0
 public void CancelLease(Lease lease, string authenticatedUserName, DateTime time)
 {
     Lease overwrite = new Lease
     {
         AuthenticatedUser = authenticatedUserName,
         License = lease.License,
         UserName = lease.UserName,
         Machine = lease.Machine,
         StartTime = lease.StartTime,
         OverwritesLease = lease,
         Grace = lease.Grace,
         EndTime = time,
     };
     this.FixLease( overwrite, time, false );
     this.Leases.InsertOnSubmit( overwrite );
 }
コード例 #11
0
 partial void InsertLease(Lease instance);
コード例 #12
0
 private void detach_Leases(Lease entity)
 {
     this.SendPropertyChanging();
     entity.License = null;
 }
コード例 #13
0
 partial void UpdateLease(Lease instance);
コード例 #14
0
 partial void DeleteLease(Lease instance);
コード例 #15
0
        private bool FixLease(Lease lease, DateTime time, bool fixEndTime = true)
        {
            PostSharp.Sdk.Extensibility.Licensing.License parsedLicense = ParsedLicenseManager.GetParsedLicense(lease.License.LicenseKey);

            if (lease.EndTime <= lease.StartTime)
                throw new Exception( "Assertion failed." );
          
            if (fixEndTime)
            {
                if ( parsedLicense.ValidTo.HasValue && parsedLicense.ValidTo < lease.EndTime )
                {
                    lease.EndTime = parsedLicense.ValidTo.Value;
                }

                if ( lease.Grace )
                {
                    DateTime graceEnd = lease.License.GraceStartTime.Value.AddDays( parsedLicense.GetGraceDaysOrDefault() );
                    if ( lease.EndTime > graceEnd )
                    {
                        lease.EndTime = graceEnd;
                    }
                }


                if ( lease.EndTime <= time )
                    return false;

                if (lease.EndTime <= lease.StartTime)
                    throw new Exception("Assertion failed.");
            }

            lease.HMAC = this.GetSignature(lease);
          

            return true;
        }
コード例 #16
0
        public Lease GetLease(Database db, string productCode, Version version, DateTime?buildDate, string machine, string userName, string authenticatedUserName, DateTime now, Dictionary <int, string> errors, License[] licenses)
        {
            Dictionary <int, LicenseState> licenseStates = new Dictionary <int, LicenseState>();

            Settings settings = Settings.Default;

            // Check if we have a lease that we can use.
            foreach (License license in licenses)
            {
                LicenseState licenseState = GetLicenseState(db, license, version, buildDate, now, licenseStates, errors);
                if (licenseState == null)
                {
                    continue;
                }



                int     licenseId     = license.LicenseId;
                Lease[] currentLeases = (from l in db.OpenLeases
                                         where
                                         l.LicenseId == licenseId &&
                                         l.StartTime <= now &&
                                         l.EndTime > now &&
                                         l.UserName == userName
                                         orderby l.StartTime
                                         select l).ToArray();

                Dictionary <string, string> machines = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase);
                foreach (Lease candidateLease in currentLeases)
                {
                    machines[candidateLease.Machine] = candidateLease.Machine;

                    if (candidateLease.Machine != machine)
                    {
                        continue;
                    }

                    if (candidateLease.EndTime > now.AddDays(settings.MinLeaseDays))
                    {
                        // We have already a good lease.
                        return(candidateLease);
                    }
                    else
                    {
                        // Invariant: we can always prolong a lease because licenses are always acquired from
                        // the present moment, therefore taking the current lease into account.
                        // UNLESS it's the end of the license period or the grace period.
                        Lease lease = db.ProlongLease(candidateLease, authenticatedUserName, now);
                        if (lease == null)
                        {
                            continue;
                        }
                        return(lease);
                    }
                }

                // We did not find a lease for the requested machines.
                // See if we can do a new lease that would not increment the number of concurrent users.
                if (machines.Count % Settings.Default.MachinesPerUser != 0)
                {
                    CheckAssertions(db, license, now);
                    Lease lease = db.CreateLease(license, userName, machine, authenticatedUserName, now, licenseState.InExcess);
                    if (lease != null)
                    {
                        return(lease);
                    }
                }
            }

            // We don't have a current lease. Create a new one.
            foreach (License license in licenses)
            {
                LicenseState licenseState = GetLicenseState(db, license, version, buildDate, now, licenseStates, errors);
                if (licenseState == null)
                {
                    continue;
                }


                if (licenseState.Maximum.HasValue && licenseState.Maximum.Value <= licenseState.Usage &&
                    (!licenseState.ParsedLicense.ValidTo.HasValue || now < licenseState.ParsedLicense.ValidTo))
                {
                    // Not enough users for this license.
                    continue;
                }


                Lease lease = db.CreateLease(license, userName, machine, authenticatedUserName, now, false);
                if (lease != null)
                {
                    return(lease);
                }
            }

            // We did not find a suitable license. See if we can use the grace period.
            foreach (License license in licenses)
            {
                LicenseState licenseState = GetLicenseState(db, license, version, buildDate, now, licenseStates, errors);

                if (licenseState == null)
                {
                    continue;
                }


                if (license.GraceStartTime == null)
                {
                    license.GraceStartTime = now;
                }

                int      graceLimit = (int)Math.Ceiling(licenseState.Maximum.Value * (100.0 + licenseState.ParsedLicense.GetGracePercentOrDefault()) / 100.0);
                DateTime graceEnd   = license.GraceStartTime.Value.AddDays(licenseState.ParsedLicense.GetGraceDaysOrDefault());

                if (license.GraceStartTime <= now && graceEnd > now && licenseState.Usage < graceLimit)
                {
                    // We will use the grace period.

                    // See if we should send a warning message.
                    if (license.GraceLastWarningTime.GetValueOrDefault(DateTime.MinValue).AddDays(settings.GracePeriodWarningDays) < now)
                    {
                        try
                        {
                            string body = string.Format(
                                "The license #{0} has a capacity of {1} concurrent user(s), but {2} users are currently using the product {3}. " +
                                "The grace period has started on {4} and will end on {5}. After this date, additional leases will be denied." +
                                "Please contact PostSharp Technologies to acquire additional licenses.",
                                license.LicenseId, licenseState.Maximum, licenseState.Usage + 1, licenseState.ParsedLicense.Product,
                                license.GraceStartTime, graceEnd);
                            const string subject = "WARNING: licensing capacity exceeded";

                            SendEmail(settings.GracePeriodWarningEmailTo, settings.GracePeriodWarningEmailCC, subject, body);
                            license.GraceLastWarningTime = now;
                        }
                        catch (Exception e)
                        {
                            Trace.TraceError(e.ToString());
                        }
                    }

                    Lease lease = db.CreateLease(license, userName, machine, authenticatedUserName, now, true);
                    if (lease != null)
                    {
                        return(lease);
                    }
                }
            }


            SendEmail(settings.DeniedRequestEmailTo, null, "ERROR: license request denied",
                      string.Format("No license with free capacity was found to satisfy the lease request for the product {0} from " +
                                    "the user '{1}' (authentication: '{2}'), machine '{3}'. " + string.Join(". ", errors.ToArray()),
                                    productCode, userName, authenticatedUserName, machine));

            return(null);
        }
コード例 #17
0
 partial void DeleteLease(Lease instance);
コード例 #18
0
 private void detach_OverwrittenByLease(Lease entity)
 {
     this.SendPropertyChanging();
     entity.OverwritesLease = null;
 }
コード例 #19
0
		private void detach_OverwrittenByLease(Lease entity)
		{
			this.SendPropertyChanging();
			entity.OverwritesLease = null;
		}
コード例 #20
0
 private void attach_Leases(Lease entity)
 {
     this.SendPropertyChanging();
     entity.License = this;
 }